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

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Vercel News
Vercel News
D
Docker
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
博客园 - 聂微东
小众软件
小众软件
Y
Y Combinator Blog
腾讯CDC
L
LangChain Blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss

博客园 - 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)  评论()    收藏  举报