2013-05-07 71 views
0

我有原型法printConstructorName一类打印构造函数的名称:如何获取子构造函数的名称?

function Parent(){ 
} 

Parent.prototype.printConstructorName = function(){ 
    console.log(this.constructor.name); 
}; 

var parent = new Parent(); 
parent.printConstructorName(); // It gets 'Parent'. 

Child继承Parent通过原型:

function Child() 
{ 
} 

Child.prototype = Parent.prototype; 

var child = new Child(); 
child.printConstructorName(); // It gets 'Parent', but 'Child' is necessary. 

如何获得孩子的构造函数的名称通过父母的原型方法?

+2

请注意,'name'是函数的不规范性,不应该使用除了调试以外的其他事情。 – Bergi 2013-05-07 14:46:13

回答

3

您分配Parent的原型对象Child.prototype - 这意味着每个子实例来自同样的事情,所有的父实例继承,当然他们继承相同constructor财产的方式。

相反,创建一个新的对象为Child.prototype,从Parent.prototype继承,并可以覆盖constructor属性,则:

Child.prototype = Object.create(Parent.prototype, { 
    constructor: {value: Child, configurable: true} 
}); 
0

也许你应该重写Child:D中的printConstructorName。

+0

谢谢。但我需要一种方法为所有的孩子) – 2013-05-07 14:45:54

4

Working fiddle

的遗传模式的问题。这里的快速修复:

function inherits(childCtor, parentCtor) { 
    function tempCtor() {}; 
    tempCtor.prototype = parentCtor.prototype; 
    childCtor.prototype = new tempCtor(); 
    childCtor.prototype.constructor = childCtor; 
}; 

function Parent(){} 

Parent.prototype.printConstructorName = function(){ 
    return this.constructor.name; 
}; 

var parent = new Parent(); 
console.log(parent.printConstructorName()); // It gets 'Parent'. 

function Child() { 
    Parent.call(this); 
}; 
inherits(Child, Parent); 

var child = new Child(); 
console.log(child.printConstructorName()); // It gets 'Child'. 
+1

你的小提琴实际上并没有工作。你忘了实际命名顶部的'inherits'函数。 – 2013-05-07 14:51:32

+0

@ alex23比我快:P +1 – 2013-05-07 14:55:16

1

当你扩展现有的构造函数,你应该在孩子的原型设置为父类的一个实例,以便更改孩子的原型不影响父母的原型。

然后,您可以简单地覆盖constructor,使其指向正确的功能。

function Parent() { 
    ....code... 
} 

Parent.prototype.printConstructorName = function() { 
    console.log(this.constructor.name); 
}; 

function Child() { 
    ...code... 
} 

Child.prototype = new Parent(); 
Child.prototype.constructor = Child; 

p = new Parent(); 
p.printConstructorName(); //Parent 

c = new Child(); 
c.printConstructorName(); //Child 
3

您的继承机制显然是错误的。你做到了,如果你添加一个属性来Child.prototype的方式,所有的父对象也将得到它...

你可能想要一个inherit功能是这样的:

inherit = (function() { 
    function F() {}; 
    return function(parent, child) { 
     F.prototype = parent.prototype; 
     child.prototype = new F(); 
     child.prototype.constructor = child; 
    }; 
}()); 

这那么你可以用这样的方式:

function Parent() { 
} 

Parent.prototype.printConstructorName = function(){ 
    console.log(this.constructor.name); 
}; 

var parent = new Parent(); 
parent.printConstructorName(); // Parent 

function Child() { 
} 

inherit(Parent, Child); 

var child = new Child(); // Child 
child.printConstructorName(); 
+0

'inherit'函数本身位于一个闭包中,它包含函数'F',但使用它不会引入其他代码中的其他闭包。 (“父母”和“孩子”不收集任何关于此类关闭的参考。) – 2013-05-13 13:04:19

1

写的扩展功能,喜欢的东西:

__extend = function(child, sup) { 
    for (prop in sup.prototype) { 
     child.prototype[prop] = sup.prototype[prop]; 
    }; 
}; 

然后调用它,而不是做prototype = new parent伎俩 - 比如:

var Parent = function() {} 
Parent.prototype.name = function() { return "name" }; 

var Child = function() {} 
__extend(Child, Parent); 

看看这个小提琴:http://jsfiddle.net/Cmg4A/

相关问题