js原型链

81 阅读1分钟
    1. 两个必要属性: 隐式原型:__proto__ 显式原型:prototype
    1. 代码demo:
class Person {
  constructor(name) {
    this.name = name;
  }
  sing() {
    console.log(`唱歌`);
  }
}
class Student extends Person {
  constructor(name, hobby) {
    super(name);
    this.hobby = hobby;
  }
  study() {
    console.log(`我叫${this.name},正在研究${this.hobby}`);
  }
}
const student1 = new Student("mao", "web前端开发");
console.log(student1.__proto__, `::: student1.__proto__`);
console.log(Student.prototype, `::: Student.prototype`);
console.log(
  student1.__proto__ === Student.prototype,
  `:::student1.__proto__ === Student.prototype`
); // true
student1.study();
student1.sing();

备注:实例上的__proto__指向类的prototype属性

  • 3. 图解:

原型链.png