vcxiaohan2 2018-05-03 06:49 采纳率: 0%
浏览 565
已结题

js原型式继承的一个问题?

                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()
  • 写回答

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();

    评论

报告相同问题?