function Animate() {
this.age = 12
}
Animate.prototype = {
say() {
console.log(this.age)
}
}
new Animate().say()
function Dog() {}
Dog.prototype = Animate.prototype// 请问这个也可以用Object.create(Animate.prototype),那么用和不用有什么区别吗,求解
let dog = new Dog()
dog.say()
js原型式继承的一个问题?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
glacierking 2018-05-03 08:00关注1.首先Animate.prototype 代表Animate的原型 而Object.create(Animate.prototype) 创建Animate的原型的子类对象 this.age是在Animate本身对象中,而非在原型中,所以 Dog.prototype = Animate.prototype要改成Dog.prototype = new Animate();
解决 无用评论 打赏 举报