原文:當(dāng)時(shí)當(dāng)我們重新定義函數(shù)的prototype時(shí)(注意:和上例的區(qū)別,這里不是修改而是覆蓋), constructor的行為就有點(diǎn)奇怪了,如下示例:function?Person(name)?{
????????????this.name?=?name;
????????};
????????Person.prototype?=?{
????????????getName:?function()?{
????????????????return?this.name;
????????????}
????????};
????????var?p?=?new?Person("haorooms");
????????console.log(p.constructor?===?Person);??//?false
????????console.log(Person.prototype.constructor?===?Person);?//?false
????????console.log(p.constructor.prototype.constructor?===?Person);?//?false為什么呢? 原來是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:Person.prototype?=?new?Object({
????????????getName:?function()?{
????????????????return?this.name;
????????????}
????????});-------------------------------------------------------------分割線----------------------------------------------------問題1.為什么重新定義prototype后p.constructor?===?Person都會(huì)返回false問題2.Person.prototype?=?new?Object({
????????????getName:?function()?{
????????????????return?this.name;
????????????}
????????});沒看懂什么意思,為什么要在寫參數(shù)的位置定義屬性方法呢
js prototype的問題
慕粉3900206
2016-10-14 16:40:17