Student.prototype.constructor = Student是什么意思啊?
Student.prototype.constructor = Student是什么意思???我理解的是將Student的prototype初始化為Student,但是沒想明白這樣做的原因。
bosn是屬于Student的,Student的prototype從Person初始化回了Student。那么問題來了,為啥bosn會(huì)追蹤到Student后繼續(xù)追蹤到之前定義的Person?
Student.prototype.constructor = Student是什么意思???我理解的是將Student的prototype初始化為Student,但是沒想明白這樣做的原因。
bosn是屬于Student的,Student的prototype從Person初始化回了Student。那么問題來了,為啥bosn會(huì)追蹤到Student后繼續(xù)追蹤到之前定義的Person?
2015-01-10
舉報(bào)
2015-01-10
該部分會(huì)在原型鏈、OOP相關(guān)得章節(jié)詳細(xì)展開討論。
簡單說,當(dāng)定義一個(gè)構(gòu)造器(函數(shù))時(shí),該構(gòu)造器就會(huì)有prototype屬性,prototype.constructor指向這個(gè)構(gòu)造器本身:
當(dāng)用該構(gòu)造器創(chuàng)建Student實(shí)例時(shí),就可以通過constructor判斷是由Student構(gòu)造的。
該constructor屬性并不是bosn這個(gè)對(duì)象上的,而是從原型鏈(Student.prototype)上來的。
當(dāng)出于實(shí)現(xiàn)繼承的目的而修改了構(gòu)造器Student.prototype時(shí),Student.prototype.constructor已經(jīng)不是Student了,為了避免誤解,手動(dòng)重設(shè)Student.prototype.constructor屬性,這樣通過new Student創(chuàng)建的實(shí)例的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;
}