js-- best inheritance


  • Micro-channel scan code number of public attention: the front end of the front end of a large front-end , the pursuit of more refined reading experience, together to learn ah
  • After Follow Send key information , free access to a complete set of front-end systems and learning materials old boy python courses
    Here Insert Picture Description

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


Published 396 original articles · won praise 786 · views 160 000 +

Guess you like

Origin blog.csdn.net/qq_42813491/article/details/102933069