如何从函数内部访问函数名?
// parasitic inheritance
var ns.parent.child = function() {
var parent = new ns.parent();
parent.newFunc = function() {
}
return parent;
}
var ns.parent = function() {
// at this point, i want to know who the child is that called the parent
// ie
}
var obj = new ns.parent.child();
可以使用name属性获取函数名,除非使用匿名函数
例如:
var Person = function Person () {
this.someMethod = function () {};
};
Person.prototype.getSomeMethodName = function () {
return this.someMethod.name;
};
var p = new Person();
// will return "", because someMethod is assigned with anonymous function
console.log(p.getSomeMethodName());
现在让我们试试named function
var Person = function Person () {
this.someMethod = function someMethod() {};
};
现在你可以使用
// will return "someMethod"
p.getSomeMethodName()