Javascript 如何获取子构造函数的名称?
我有一个原型方法为Javascript 如何获取子构造函数的名称?,javascript,Javascript,我有一个原型方法为printConstructorName的类,它打印构造函数的名称: function Parent(){ } Parent.prototype.printConstructorName = function(){ console.log(this.constructor.name); }; var parent = new Parent(); parent.printConstructorName(); // It gets 'Parent'. 类子类通过原型继承
printConstructorName
的类,它打印构造函数的名称:
function Parent(){
}
Parent.prototype.printConstructorName = function(){
console.log(this.constructor.name);
};
var parent = new Parent();
parent.printConstructorName(); // It gets 'Parent'.
类子类
通过原型继承父类:
function Child()
{
}
Child.prototype = Parent.prototype;
var child = new Child();
child.printConstructorName(); // It gets 'Parent', but 'Child' is necessary.
如何通过父类的prototype方法获取子类构造函数的名称?也许您应该覆盖Child:D中的printConstructorName。您正在将父类的prototype对象分配给子类。prototype-这意味着每个子类实例继承自与所有父类实例相同的对象,当然,它们以这种方式继承了相同的
构造函数属性
相反,为子.prototype
创建一个新对象,继承自父.prototype
,您可以覆盖构造函数
属性,然后:
Child.prototype = Object.create(Parent.prototype, {
constructor: {value: Child, configurable: true}
});
继承模式是个问题。以下是快速修复方法:
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'.
在扩展现有构造函数时,应该将子原型设置为父类的实例,这样子原型的更改就不会影响父原型
然后您可以简单地重写构造函数
,使其指向正确的函数
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
你的继承机制显然是错误的。按照您的方式,如果向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();
编写一个扩展函数,类似于:
__extend = function(child, sup) {
for (prop in sup.prototype) {
child.prototype[prop] = sup.prototype[prop];
};
};
然后调用它,而不是执行prototype=newparent
trick-like:
var Parent = function() {}
Parent.prototype.name = function() { return "name" };
var Child = function() {}
__extend(Child, Parent);
看看这把小提琴:谢谢。但是我需要为所有孩子提供一种方法)请注意,name
是函数的非标准属性,不应用于调试以外的任何其他用途。您的小提琴实际上不起作用。您忘记在顶部实际命名inherits
函数。@alex23比我快:P+1inherit
函数本身在一个闭包中,该闭包包含函数F
,但使用它不会在其他代码中引入额外的闭包。(父级
和子级
不收集对此类闭包的任何引用。)