惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
U
Unit 42
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
C
Check Point Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
Vercel News
Vercel News
腾讯CDC
GbyAI
GbyAI
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件

博客园 - howhy

TypeScript any vs unknown 详细对比 TypeScript interface vs type 完整对比 html 元素包含关系 this 指向 空对象 Object.keys for ...in Reflect.ownKeys ...区别 逻辑运算符和空值运算符 运算符优先级 js 类型显式转换 js 高级函数 js 方法重载 fetch timeout js 任务顺序执行 暂停 js 并发任务 判断两个对象是否相同 js 动态拦截属性 js 通用动画 js groupby js 防抖和节流 实现 instanceof 操作符 js 单例模式 js 数组去重和扁平方法 js new的过程实现 js deepCopy js '=='的隐性类型转换规则
js 继承方法
howhy · 2025-12-26 · via 博客园 - howhy
function Animal(name) {
    this.name = name;
}

Animal.prototype.speak = function() {
    console.log(this.name + ' makes a noise');
};

function Dog(name, breed) {
    // 实现继承
    Animal.call(this,name);
    this.breed=breed;
}
//方法1 
// Dog.prototype=Object.create(Animal.prototype);
// Dog.prototype.constructor=Dog;
//方法二
inherit(Dog,Animal)
function inherit(child,parent){
    const proto=Object.create(parent.prototype);
    child.prototype=proto;
    child.prototype.constructor=child;

}
Dog.prototype.bark=function(){
    console.log(this.name+ ' barks')
}


// 实现 Dog 继承 Animal,并添加 bark 方法
const dog = new Dog('Buddy', 'Golden');
dog.speak(); // Buddy makes a noise
dog.bark();  // Buddy barks!
console.log(dog instanceof Dog); // true
console.log(dog instanceof Animal); // true

posted @ 2025-12-26 15:28  howhy  阅读(26)  评论()    收藏  举报