ES5中类的继承

2019-11-5 张宇童 学习日志

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类中

标签: javascript ES5 继承

发表评论:

Powered by emlog