第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Student.prototype.constructor = Student是什么意思???

Student.prototype.constructor = Student是什么意思?。课依斫獾氖菍tudent的prototype初始化為Student,但是沒想明白這樣做的原因。

bosn是屬于Student的,Student的prototype從Person初始化回了Student。那么問題來了,為啥bosn會追蹤到Student后繼續(xù)追蹤到之前定義的Person?

正在回答

2 回答

該部分會在原型鏈、OOP相關(guān)得章節(jié)詳細(xì)展開討論。

簡單說,當(dāng)定義一個構(gòu)造器(函數(shù))時,該構(gòu)造器就會有prototype屬性,prototype.constructor指向這個構(gòu)造器本身:

function?Student()?{
}
Student.prototype.constructor?===?Student;?//?true


當(dāng)用該構(gòu)造器創(chuàng)建Student實例時,就可以通過constructor判斷是由Student構(gòu)造的。

var?bosn?=?new?Student();
bosn.constructor?===?Student;//?true


該constructor屬性并不是bosn這個對象上的,而是從原型鏈(Student.prototype)上來的。

bosn.hasOwnProperty('constructor');?//?false


當(dāng)出于實現(xiàn)繼承的目的而修改了構(gòu)造器Student.prototype時,Student.prototype.constructor已經(jīng)不是Student了,為了避免誤解,手動重設(shè)Student.prototype.constructor屬性,這樣通過new Student創(chuàng)建的實例的constructor又可以正確取道Student了。


更多詳情,關(guān)注后續(xù)課程更新吧:)

9 回復(fù) 有任何疑惑可以回復(fù)我~

我給你測試了一下

Student.prototype = new Person;

Student.prototype.constructor?= Student; ( 這句不寫)

console.log(Student.prototype.constructor);

//

? Person(name,age){

? ? this.name = name;

? ? this.age = age;

}

>>>>>>>>>>

Student.prototype.constructor?= Student; ( 寫上這句)

//

? Student(name,age,className){

? ? Person.call(this,name,age);

? ? this.className = className;

}

>>>>>>>>>>

Student.prototype.constructor?= Person; ( 指向Person)

//

? Person(name,age){

? ? this.name = name;

? ? this.age = age;

}



0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
JavaScript深入淺出
  • 參與學(xué)習(xí)       281098    人
  • 解答問題       1048    個

由淺入深學(xué)習(xí)JS語言特性,且解析JS常見誤區(qū),從入門到掌握

進(jìn)入課程

Student.prototype.constructor = Student是什么意思???

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號