有没有一种方法可以在被调用者中获取调用者函数的名称
Is there a way to get the name of the caller function within the callee?
可能重复:
Javascript如何找到调用方函数?
今天早上,我尝试了javascript/jQuery,并试图捕获当前正在执行的函数的调用方名称。
因此,在下面的示例中,日志将显示runMe
作为调用者,showMe
作为被调用者。
jQuery(document).ready(function($) {
function showMe() {
// should log the runMe as the caller and showMe as callee
console.log('Callee: ',arguments.callee)
console.log('Caller: ',arguments.caller);
}
function runMe() {
// getting executed as a button is pressed.
showMe();
}
$('a').bind('click', function(e) {
e.preventDefault();
runMe();
});
});
以上显然不起作用,因此我向大家提出问题。
在上面的例子中,有没有一个好的方法来获取调用者?
请注意:我知道我可以得到runMe
的被调用者,并将其作为参数传递到showMe
中,但这个问题的目的是找到一个不需要"手动"将调用者传递到函数中的解决方案
有什么理由不做这样的事吗?
您过去可以执行arguments.caller.name
,但在Javascript 1.3中已弃用。
arguments.callee.caller.name
(或者仅仅是showMe.caller.name
(是另一条路。这是非标准的,在严格模式下不受支持,但目前所有主要浏览器都支持(ref(。
像这个一样尝试callee.caller
function showMe() {
// should log the runMe as the caller and showMe as callee
console.log('Callee: ',arguments.callee.name)
console.log('Caller: ',arguments.callee.caller.name);
}
这对你有用吗?
function showMe() {
// should log the runMe as the caller and showMe as callee
console.log('Callee: ',arguments.callee)
console.log('Caller: ',arguments.callee.caller);
}
注意,这是非标准的javascript。
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/caller
我觉得是。。。。
arguments.callee.caller
相关文章:
- Javascript-如何获取被点击的元素
- 你能调用一个在调用者上下文中返回的函数吗
- Web Worker postMessage({})何时返回给调用者
- 水平滚动Marquee,当函数被调用时,我的文本坚持聚集在一起
- 有没有一种方法可以让watch函数获取被监视文件的名称
- 当javascript是单线程时,谁是onreadystatechange的调用者
- HTML5如何获取被点击对象的id
- 函数调用者无效元素
- 用被调用者的尾部调用函数'的参数列表
- javascript可以在调用者之后定义被调用者,以及如何解析它
- 如何从严格模式获取调用者
- 是否可以在javascript中获取调用者上下文
- 主干自定义事件:获取调用者对象
- JavaScript获取原始调用者对象
- 获取函数调用者的作用域
- Promise的第一个调用者应该获取数据,随后的调用者应该等待
- Javascript:我如何在JavaScript中获取被调用者ID
- 有没有一种方法可以在被调用者中获取调用者函数的名称
- 在ExtJS的被调用类中引用调用者类
- 无法获取函数调用者名称(arguments.callee).呼叫者不工作)