我有原型法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.
如何获得孩子的构造函数的名称通过父母的原型方法?
请注意,'name'是函数的不规范性,不应该使用除了调试以外的其他事情。 – Bergi 2013-05-07 14:46:13