原型和构造函数对象属性 [英] prototype and constructor object properties
问题描述
我有:
function Obj1(param)
{
this.test1 = param || 1;
}
函数Obj2(param,par)
{
this.test2 = param;
}
b
Obj2.prototype = new Obj1(44);
var obj = new Obj2(55);
alert(obj.constructor)
我有:
function Obj1(param){
this.test1 = param || 1;
}
但构造函数已经是Obj2 ...为什么?
Obj1已经成为Obj2的原型...
有人可以解释我的细节,原型链和构造函数属性
感谢
构造函数
原型对象的常规属性( DontEnum
标志设置,因此它不会出现在 for..in
循环)。如果替换原型对象,构造函数
属性也将被替换 - 参见此解释了解详情。
您可以通过手动设置 Obj2.prototype.constructor = Obj2
,但是这样, DontEnum
标志不会设置。
由于这些问题,依靠构造函数
进行类型检查不是一个好主意:use instanceof
或 isPrototypeOf()
。
Andrey Fedorov提出了为什么 new
不会将构造函数
属性分配给实例对象的问题。我猜这是为什么是沿着以下几行:
从同一个构造函数创建的所有对象共享构造函数属性,共享属性驻留在原型。 p>
真正的问题是JavaScript没有内置的支持继承层次结构。有几个方法解决这个问题(你的是其中之一),另一个在精神中的JavaScript将是以下:
function addOwnProperties(obj / *,... * /){
for(var i = 1; i var current =一世];
for(var prop in current){
if(current.hasOwnProperty(prop))
obj [prop] = current [prop]
}
}
}
函数Obj1(arg1){
this.prop1 = arg1 || 1;
}
Obj1.prototype.method1 = function(){};
function Obj2(arg1,arg2){
Obj1.call(this,arg1);
this.test2 = arg2 || 2;
}
addOwnProperties(Obj2.prototype,Obj1.prototype);
Obj2.prototype.method2 = function(){};
这使得多重继承也很简单。
I've:
function Obj1(param)
{
this.test1 = param || 1;
}
function Obj2(param, par)
{
this.test2 = param;
}
now when I do:
Obj2.prototype = new Obj1(44);
var obj = new Obj2(55);
alert(obj.constructor)
I have:
function Obj1(param) {
this.test1 = param || 1;
}
but the constructor function has been Obj2... why that? Obj1 has become the Obj2 prototype...
Can someone explain me, in detail, the prototype chain and the constructor property
Thanks
constructor
is a regular property of the prototype object (with the DontEnum
flag set so it doesn't show up in for..in
loops). If you replace the prototype object, the constructor
property will be replaced as well - see this explanation for further details.
You can work around the issue by manually setting Obj2.prototype.constructor = Obj2
, but this way, the DontEnum
flag won't be set.
Because of these issues, it isn't a good idea to rely on constructor
for type checking: use instanceof
or isPrototypeOf()
instead.
Andrey Fedorov raised the question why new
doesn't assign the constructor
property to the instance object instead. I guess the reason for this is along the following lines:
All objects created from the same constructor function share the constructor property, and shared properties reside in the prototype.
The real problem is that JavaScript has no built-in support for inheritance hierarchies. There are several ways around the issue (yours is one of these), another one more 'in the spirit' of JavaScript would be the following:
function addOwnProperties(obj /*, ...*/) {
for(var i = 1; i < arguments.length; ++i) {
var current = arguments[i];
for(var prop in current) {
if(current.hasOwnProperty(prop))
obj[prop] = current[prop];
}
}
}
function Obj1(arg1) {
this.prop1 = arg1 || 1;
}
Obj1.prototype.method1 = function() {};
function Obj2(arg1, arg2) {
Obj1.call(this, arg1);
this.test2 = arg2 || 2;
}
addOwnProperties(Obj2.prototype, Obj1.prototype);
Obj2.prototype.method2 = function() {};
This makes multiple-inheritance trivial as well.
这篇关于原型和构造函数对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!