Nat*_*man 50 javascript function
抱歉这个非常奇怪的标题,但这就是我要做的事情:
var f1 = function (param1, param2) {
// Is there a way to get an object that is ‘f1’
// (the current function)?
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我想从匿名函数中访问当前函数.
这可能吗?
ami*_*mik 48
说出来.
var f1 = function fOne() {
console.log(fOne); //fOne is reference to this function
}
console.log(fOne); //undefined - this is good, fOne does not pollute global context
Run Code Online (Sandbox Code Playgroud)
Chr*_*ann 32
是 - arguments.callee是当前的功能.
注意:这在ECMAScript 5中已弃用,并且可能会导致尾调用递归等性能受到影响.但是,它在大多数主流浏览器中都有效.
在你的情况下,f1也将工作.
您可以访问它,f1因为在调用函数f1 之前,函数将被分配给变量:
var f1 = function () {
f1(); // Is valid
};
f1(); // The function is called at a later stage
Run Code Online (Sandbox Code Playgroud)