prototype的問題
?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
2016-11-11
答案是:
Horse.prototype===horse.__proto__
Mammal.prototype===Horse.prototype.__proto__
Animal.prototype===Mammal.prototype.__proto__
本來(lái)我問問題的疑惑是 不清楚prototype ,proto和constructor它們的作用和意義
上面例子Animal,Mammal,Horse的prototype都被重寫了,Animal.prototype.constructor!==Animal ,Mammal和Horse也是一樣,__proto__就是[[proto]]也就是對(duì)象的指針,這個(gè)對(duì)象指針指向自己構(gòu)造函數(shù)的prototype.
2016-11-09
你的問號(hào)是什么意思