class Person{ constructor(name){ this.name = name; } greet(){ console.log('Hello,my name is ' + this.name + 'and I am ' + this.age); } }class Max extends Person { constructor(age){ super('Max'); this.age = age; } greet(){ console.log('haha'); } greetTwice(){ super.greet(); this.greet(); } } let max = new Max(27); max.greetTwice(); console.log(max.__proto__ == Max.prototype); //true; console.log(max.__proto__ == Person.prototype); //false;为啥Max是继承了Person,但是他们的prototype却不一样?
1 回答
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
要是相等才怪呢!!!
帮你画了一张图,对于class Person和class Max有:
从图中可以得出:
max.__proto__ === Max.prototype;
max.__proto__.__proto__ === Max.prototype.__proto__ === Person.prototype === Max.__proto__.prototype
添加回答
举报
0/150
提交
取消