js--最优继承


  • 微信扫码关注公众号 :前端前端大前端,追求更精致的阅读体验 ,一起来学习啊
  • 关注后发送关键资料,免费获取一整套前端系统学习资料和老男孩python系列课程
    在这里插入图片描述

function Person(name) {
    this.name = name;
}

Person.prototype.say=function(){
    console.log(`My name is ${this.name}`)
}

function Student(name) {
    Person.call(this, name)
}

// Object.create方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__
Student.prototype=Object.create(Person.prototype);
// 修正构造函数指向 Person=>Student
Student.prototype.constructor=Student;

var stu=new Student("tom");

stu.say()//My name is tom


发布了396 篇原创文章 · 获赞 786 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/qq_42813491/article/details/102933069
今日推荐