在javascript中确定调用函数 [英] Determine calling function in javascript

查看:120
本文介绍了在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 the arguments.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 deprecated arguments.caller property directly, which is not implemented in some modern browsers.

这篇关于在javascript中确定调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆