原文:當時當我們重新定義函數的prototype時(注意:和上例的區別,這里不是修改而是覆蓋), constructor的行為就有點奇怪了,如下示例: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為什么呢? 原來是因為覆蓋Person.prototype時,等價于進行如下代碼操作:Person.prototype?=?new?Object({
????????????getName:?function()?{
????????????????return?this.name;
????????????}
????????});-------------------------------------------------------------分割線----------------------------------------------------問題1.為什么重新定義prototype后p.constructor?===?Person都會返回false問題2.Person.prototype?=?new?Object({
????????????getName:?function()?{
????????????????return?this.name;
????????????}
????????});沒看懂什么意思,為什么要在寫參數的位置定義屬性方法呢
js prototype的問題
慕粉3900206
2016-10-14 16:40:17