Student.prototype.constructor = Student是什么意思???
Student.prototype.constructor = Student是什么意思?。课依斫獾氖菍tudent的prototype初始化為Student,但是沒想明白這樣做的原因。
bosn是屬于Student的,Student的prototype從Person初始化回了Student。那么問題來了,為啥bosn會追蹤到Student后繼續(xù)追蹤到之前定義的Person?
Student.prototype.constructor = Student是什么意思?。课依斫獾氖菍tudent的prototype初始化為Student,但是沒想明白這樣做的原因。
bosn是屬于Student的,Student的prototype從Person初始化回了Student。那么問題來了,為啥bosn會追蹤到Student后繼續(xù)追蹤到之前定義的Person?
2015-01-10
舉報
2015-01-10
該部分會在原型鏈、OOP相關(guān)得章節(jié)詳細(xì)展開討論。
簡單說,當(dāng)定義一個構(gòu)造器(函數(shù))時,該構(gòu)造器就會有prototype屬性,prototype.constructor指向這個構(gòu)造器本身:
當(dāng)用該構(gòu)造器創(chuàng)建Student實例時,就可以通過constructor判斷是由Student構(gòu)造的。
該constructor屬性并不是bosn這個對象上的,而是從原型鏈(Student.prototype)上來的。
當(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ù)課程更新吧:)
2018-08-07
我給你測試了一下
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;
}