【发布时间】:2023-03-17 00:43:01
【问题描述】:
我有一个带有原型方法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.
如何通过Parent的原型方法获取Child的构造函数名称?
【问题讨论】:
-
请注意
name是函数的非标准属性,不能用于除调试以外的任何用途。
标签: javascript