确定在JavaScript [复制]调用函数
-
20-09-2019 - |
题
<强>可能重复:强>结果 你如何找出调用函数在JavaScript?
我怎样才能找出一个JavaScript函数这是调用(前者在调用堆栈)功能?
我想确定如果前者称为功能是在onbeforeunload事件一个__doPostBack。
解决方案
每个函数都有一个caller
属性定义
从 https://developer.mozilla.org/en/Core_JavaScript_1 .5_Reference / Global_Objects /功能/呼叫者:
function myFunc() {
if (myFunc.caller == null) {
return ("The function was called from the top!");
} else
return ("This function's caller was " + myFunc.caller);
}
}
在Function.caller
属性不是ECMA3标准的一部分,但它在所有主要浏览器,包括IE和Firefox实现。
如果您使用的是匿名函数,你仍然可以通过的caller属性=“https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments/callee”相对= “noreferrer”> arguments.calee
属性:
function() {
if (arguments.callee.caller == null) {
return ("The function was called from the top!");
} else
return ("This function's caller was " + arguments.callee.caller);
}
}
请注意,此代码访问当前功能,然后引用在其上的相同的非标准caller
属性。这是从使用的弃用 arguments.caller
不同一>属性直接,这是未实现在一些现代浏览器。
其他提示
在上CR-48 Chrome操作系统,arguments.callee.caller给出整个身体功能作为一个字符串,对于命名姓名的打电话人的功能。
不隶属于 StackOverflow