JS继承实现

9 天前(已编辑)

JS继承实现

7.手动实现ES5的继承

function People() {
    this.type = 'prople'
  }

People.prototype.eat = function () {
    console.log('吃东西啦');
}

function Man(name) {
    this.name = name;
    this.color = 'black';
}

No.1原型继承

Man.prototype = new People();
let a = new Man()
console.log(a.__proto__)

No.2构造函数继承

function Man(name){
    People.call(this)
}

No.3组合继承

function Man(name){
    People.call(this)
}
Man.prototype = People.prototype

No.4寄生组合继承

function Man(name){
    People.call(this)
}
Man.prototype = Object.create(People.prototype, {
    constructor: {
        value: Man
    }
})

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...