[英]How can I get a function name when it is being called?
I'm not sure if this is a good idea or not, but I'd like to be able to get the name of a function from within it, while it is being called. 我不确定这是否是个好主意,但是我希望能够在调用函数时从函数中获取函数的名称。 The test code I wrote below seems to work, in that it doesn't cause a name clash, but I can't get the name of the function without knowing it first.
我在下面编写的测试代码似乎可以正常工作,因为它不会引起名称冲突,但是在不先了解函数的情况下我无法获得该函数的名称。
function And(){
return "test";
};
var X = {
And: function And(){
return this.And.name;
}
};
document.write(X.And());
Is there any way of achieving this that doesn't involve binding the context (ie the value of 'this') on the function? 有什么方法可以实现,而无需在函数上绑定上下文(即“ this”的值)?
Simply by using this aguments.callee.name; 只需使用此aguments.callee.name;
try this example : 试试这个例子:
function myFunc()
{
return arguments.callee.name;
}
Note that the arguments.callee will be depricated 请注意arguments.callee将被描述
Function.caller or arguments.callee.name Function.caller或arguments.callee.name
However arguments.callee() doesn't work under ES5. 但是arguments.callee()在ES5下不起作用。 And Function.caller is not standard.
而且Function.caller不是标准的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.