Javascript 使用constructor.prototype 访问超类成员失败?
Javascript using constructor.prototype to visit superclass member fails?
我有一个小代码片段:
function father(){
this.id=10
}
function child(){}
console.log(child.prototype.constructor);//[Function: child]
child.prototype=new father();
//child.prototype.constructor = child; // key line
var son=new child();
console.log(child.prototype.constructor);//[Function: father]
console.log(son.constructor.prototype.id);//undefined.
如代码所示,我使用原型链创建了 "son" 对象。
但是最后一行打印
"undefined".
这对我来说很奇怪。 child.prototype.constructor 是 [Function: father],而 "id" 实际上是 "father" 的 属性 为什么它打印未定义?
如果我取消注释
child.prototype.constructor = child; // key line
然后它如我所料打印“10”。有没有关键线的区别,对我来说,是 child.prototype.constructor 是 'child' 或 'father'。但是'id'是爸爸属性,为什么要在我的代码中设置关键行?
谢谢。
实际上,您正在覆盖子函数() 的原型对象,因此内部 proto link 丢失了。
function father(){
this.id=10
}
function child(){}
console.log(child.prototype.constructor);//[Function: child]
child.prototype=new father();
child.__proto__ = father;
var son=new child();
console.log(child.prototype.constructor);//[Function: father]
console.log(son.id);//10
步骤 1)
function father(){
this.id=10
}
function child(){}
看起来像
在这种情况下 console.log(child.prototype.constructor);//[Function: child]
它会按您预期的那样工作
步骤 2) child.prototype=new father();
现在看到这里,原来的child.prototype
丢了,child.prototype.constructor
也丢了。您从 father
创建了一个对象并将该对象用作 child.prototype
步骤 3) var son=new child();
现在console.log(child.prototype.constructor);//[Function: father]
就直接理解了。
我们如何到达那里? son.__proto__.__proto__.constructor
.
现在,考虑相同的图像
console.log(son.constructor.prototype.id);//undefined.
这里发生了什么? son.constructor
只不过是 father
而 son.constructor.prototype
只不过是 father.prototype
,它没有 属性 名称 id
。
注意:
son.constructor
是 son.__proto__.__proto__.constructor
现在你取消注释 child.prototype.constructor = child;
会发生什么?
您正在向 child.prototype
添加 constructor
属性,在这种情况下,当您说 son.constructor
时,这意味着 child
和 son.constructor.prototype
只不过是 child.prototype
,它确实有一个 属性 名称 id
并且值为 10。
抱歉所有图片和错误的解释!!
我有一个小代码片段:
function father(){
this.id=10
}
function child(){}
console.log(child.prototype.constructor);//[Function: child]
child.prototype=new father();
//child.prototype.constructor = child; // key line
var son=new child();
console.log(child.prototype.constructor);//[Function: father]
console.log(son.constructor.prototype.id);//undefined.
如代码所示,我使用原型链创建了 "son" 对象。 但是最后一行打印
"undefined".
这对我来说很奇怪。 child.prototype.constructor 是 [Function: father],而 "id" 实际上是 "father" 的 属性 为什么它打印未定义?
如果我取消注释
child.prototype.constructor = child; // key line
然后它如我所料打印“10”。有没有关键线的区别,对我来说,是 child.prototype.constructor 是 'child' 或 'father'。但是'id'是爸爸属性,为什么要在我的代码中设置关键行?
谢谢。
实际上,您正在覆盖子函数() 的原型对象,因此内部 proto link 丢失了。
function father(){
this.id=10
}
function child(){}
console.log(child.prototype.constructor);//[Function: child]
child.prototype=new father();
child.__proto__ = father;
var son=new child();
console.log(child.prototype.constructor);//[Function: father]
console.log(son.id);//10
步骤 1)
function father(){
this.id=10
}
function child(){}
看起来像
在这种情况下 console.log(child.prototype.constructor);//[Function: child]
它会按您预期的那样工作
步骤 2) child.prototype=new father();
现在看到这里,原来的child.prototype
丢了,child.prototype.constructor
也丢了。您从 father
创建了一个对象并将该对象用作 child.prototype
步骤 3) var son=new child();
现在console.log(child.prototype.constructor);//[Function: father]
就直接理解了。
我们如何到达那里? son.__proto__.__proto__.constructor
.
现在,考虑相同的图像
console.log(son.constructor.prototype.id);//undefined.
这里发生了什么? son.constructor
只不过是 father
而 son.constructor.prototype
只不过是 father.prototype
,它没有 属性 名称 id
。
注意:
son.constructor
是 son.__proto__.__proto__.constructor
现在你取消注释 child.prototype.constructor = child;
会发生什么?
您正在向 child.prototype
添加 constructor
属性,在这种情况下,当您说 son.constructor
时,这意味着 child
和 son.constructor.prototype
只不过是 child.prototype
,它确实有一个 属性 名称 id
并且值为 10。
抱歉所有图片和错误的解释!!