在javascript中确定调用函数 [英] Determine calling function in javascript
问题描述
如何在JavaScript函数中找到调用函数(前者在调用堆栈中)?
I想要确定前面调用的函数是否是onbeforeunload事件中的__doPostback。
每个函数都有一个调用者
定义的属性。
来自 https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/caller :
函数myFunc(){
属性。
if(myFunc.caller == null){
return(函数被调用从一开始!);
} else
return(这个函数的调用者是+ myFunc.caller);
$ b $Function.caller
属性不是ECMA3标准的一部分,但它在所有主流浏览器(包括IE和Firefox)上实现。
如果您使用匿名函数,您仍然可以通过 caller
<$ c $ c> function(){
if(arguments.callee.caller == null){
return(函数从顶部调用!
} else
return(这个函数的调用者是+ arguments.callee.caller);
$ / code $ / pre
注意这段代码正在访问当前函数,然后引用相同的非标准调用者
属性。这与使用弃用的arguments.caller
属性,在一些现代浏览器中未实现。Possible Duplicate:
How do you find out the caller function in JavaScript?How can I find out in a javascript function which was the calling (the former in the call stack) function?
I would like to determine if the former called function is a __doPostback in the onbeforeunload event.
解决方案Each function has a
caller
property defined.From https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/caller:
function myFunc() { if (myFunc.caller == null) { return ("The function was called from the top!"); } else return ("This function's caller was " + myFunc.caller); } }
The
Function.caller
property is not part of the ECMA3 standard but it's implemented across all major browsers, including IE and Firefox.If you're using an anonymous function, you can still access the
caller
property via thearguments.calee
property:function() { if (arguments.callee.caller == null) { return ("The function was called from the top!"); } else return ("This function's caller was " + arguments.callee.caller); } }
Note that this code is accessing the current function, and then referencing the same non-standard
caller
property on it. This is distinct from using the deprecatedarguments.caller
property directly, which is not implemented in some modern browsers.这篇关于在javascript中确定调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!