简体   繁体   English

如何获得子构造函数的名称?

[英]How to get name of child constructor?

I have a class with prototype method printConstructorName which prints the name of the constructor: 我有一个带有原型方法printConstructorName的类, printConstructorName打印构造函数的名称:

function Parent(){
}

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

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

A class Child inherits the Parent through the prototype: Child类通过原型继承了Parent

function Child()
{
}

Child.prototype = Parent.prototype;

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

How to get the name of Child's constructor through the Parent's prototype method? 如何通过Parent的原型方法获得Child的构造函数的名称?

Working fiddle 工作提琴

The inheritance pattern is the problem. 继承模式就是问题所在。 Here's the quick fix: 快速修复方法如下:

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'.

You are assigning the Parent s prototype object to Child.prototype - which means that every child instance inherits from the same thing as all parent instances, and of course they inherit the same constructor property that way. 您正在将Parent的原型对象分配给Child.prototype -这意味着每个子实例都继承自与所有父实例相同的事物,当然,它们也以这种方式继承相同的constructor属性。

Instead, create a new object for the Child.prototype , inheriting from the Parent.prototype , and you can overwrite the constructor property then: 而是为Child.prototype创建一个新对象,该对象继承自Parent.prototype ,然后可以覆盖constructor属性:

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

Your inheritance mechanism is clearly wrong. 您的继承机制显然是错误的。 The way you've done it, if you add a property to Child.prototype, all Parent objects will also get it... 完成此操作的方式,如果将属性添加到Child.prototype,则所有Parent对象也将获取它。

You might want an inherit function something like this: 您可能想要一个inherit函数,如下所示:

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

which you can then use this way: 然后您可以使用这种方式:

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();

When you're extending an existing constructor, you should set the child prototype to an instance of the parent class, so that changes to the child prototype don't affect the parent prototype. 扩展现有构造函数时,应将子原型设置为父类的实例,以便对子原型的更改不会影响父原型。

Then you can simply override the constructor such that it points to the correct function. 然后,您可以简单地重写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

Write an extend function, something like: 编写一个扩展函数,类似于:

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

And then you call it instead of doing the prototype = new parent trick - like: 然后调用它,而不是执行prototype = new parent技巧-像这样:

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

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

Check out this fiddle: http://jsfiddle.net/Cmg4A/ 看看这个小提琴: http : //jsfiddle.net/Cmg4A/

也许您应该在Child:D中覆盖printConstructorName。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM