ES5中类的继承

43 次浏览 1 分钟阅读 0 个喜欢

ES5中类的继承实现相对是比较麻烦的

let Animal = function (type) {
    this.type = type
}

Animal.prototype.eat = function () {
    console.log('我要吃')
}

let Dog = function () {
    Animal.call(this, 'dog')
}

Dog.prototype = Animal.prototype

let dog = new Dog()
dog.eat()

这里需要说明的是call(this, args)
call是指将Animal中的this指向Dog类中