亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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

問題:

  1. Horse.prototype===?為true

  2. Mammal.prototype===?為true

  3. Animal.prototype===?為true

正在回答

2 回答

答案是:

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.

1 回復 有任何疑惑可以回復我~

你的問號是什么意思


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
JavaScript深入淺出
  • 參與學習       281094    人
  • 解答問題       1054    個

由淺入深學習JS語言特性,且解析JS常見誤區,從入門到掌握

進入課程

prototype的問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號