为了使画面清晰:
在链中唯一
obj->"new B()"->"new A()" // where obj is the same as "new C()"
"new A()"
对象有财产constructor
。所有其他对象从原型链获得constructor
属性。
在代码:
var A = function(){
}
A.prototype.name="A";
// A had not create "constructor" property for "new A()"
// so, looking at the prototype
// According to ECMAScript spec 13.2.10
// A.prototype.constructor == A
// thus
// "new A()".constructor == A
var B = function(){
}
B.prototype = new A();
B.prototype.name="B";
// B had not create "constructor" property for "new B()"
// looking at the prototype
// B.prototype is "new A()"
// as shown above
// "new A()".constructor == A
// thus
// B.prototype.constructor == A
// and at the end
// "new B()".constructor == A
var C = function(){
}
C.prototype = new B();
C.prototype.name="C";
// C had not create "constructor" property for "new C()"/"obj"
// looking at the prototype
// C.prototype is "new B()"
// looking up to "new B()".prototype
// "new B()".prototype is "new A()" as shown above
// "new A()".constructor == A
// and finally
// C.prototype.constructor == A
obj = new C();
print(obj.name);
print(obj.constructor.prototype.name);
print(obj.constructor == A);
因此,作为写mellamokb我们应该覆盖(如果更精确的创建,)constructor
财产。
构造函数属性在原型对象中定义,并且在为其分配时指定其所有成员。任何你想拥有不同值的成员都必须被定义,否则你将继承构造函数,toString,valueOF,以及原型所包含的其他东西。 – kennebec 2011-05-13 16:07:09
谢谢,我意识到这已经 – nahab 2011-05-13 16:20:08