js 继承

71 阅读1分钟

es6继承

class Person {
  constructor(name, age) {
    this.name = name
    this.age = age
  }
  eat() {
    console.log(`我是aa,我会{this.name}`)
  }
}

class teacher extends Person {
  constructor(name, age, hobby) {
    // 执行父类的构造器
    super(name, age)
    this.hobby = hobby
  }
  swim() {
    console.log(this.name + this.age + this.hobby)
  }
}
const aa = new teacher('张三', '12', '唱跳')
console.log(aa.swim())

es5继承

寄生组合继承

image.png

组合继承

image.png