javascript - 关于es6 class的问题
本文介绍了javascript - 关于es6 class的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
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却不一样?
解决方案
要是相等才怪呢!!!
帮你画了一张图,对于class Person和class Max有:
从图中可以得出:
max.__proto__ === Max.prototype;
max.__proto__.__proto__ === Max.prototype.__proto__ === Person.prototype === Max.__proto__.prototype
这篇关于javascript - 关于es6 class的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文