慕设计2087194
2016-11-09 02:51
function Animal(name) {
this.name = name;
}
Animal.prototype = {
weight: 0,
eat: function() {
alert( "Animal is eating!" );
}
}
function Mammal() {
this.name = "mammal";
}
Mammal.prototype = new Animal("animal");
function Horse( height, weight ) {
this.name = "horse";
this.height = height;
this.weight = weight;
}
Horse.prototype = new Mammal();
Horse.prototype.eat = function() {
alert( "Horse is eating grass!" );
}
var horse = new Horse( 100, 300 );
问题:
Horse.prototype===?为true
Mammal.prototype===?为true
Animal.prototype===?为true
答案是:
Horse.prototype===horse.__proto__
Mammal.prototype===Horse.prototype.__proto__
Animal.prototype===Mammal.prototype.__proto__
本来我问问题的疑惑是 不清楚prototype ,proto和constructor它们的作用和意义
上面例子Animal,Mammal,Horse的prototype都被重写了,Animal.prototype.constructor!==Animal ,Mammal和Horse也是一样,__proto__就是[[proto]]也就是对象的指针,这个对象指针指向自己构造函数的prototype.
你的问号是什么意思
JavaScript深入浅出
281100 学习 · 1020 问题
相似问题