原型与原型链

51 阅读1分钟

使用class实现继承

//共同属性写到父类构造函数中
class Person{
  constructor(name){
    this.name = name;
  }
}
//用extends继承父类
class Student extends Person{
  constructor(name, score) {
    super(name);  //执行父类构造函数
    this.score = score;
  }
​
  introduce() {
    console.log(`我是${this.name},考了${this.score}分`);
  }
}
​
const student = new Student('张三', 99);  //实例化这个类
student.introduce();
​
class Teacher extends Person{
  constructor(name, score) {
    super(name);
    this.subject = subject;
  }
​
  teach() {
    console.log(`我是${this.name},${this.subject}`);
  }
}
​
const Teacher = new Teacher('张三', '前端');
Teacher.teach()

隐式原型

__proto__ 显式原型上没有的方法就会在该隐式原型中

1706153467057.png

1706153535699.png

原型链

1706153914535.png