<分区>
Possible Duplicate:
Javascript how do you find the caller function?
今天早上我正在尝试使用 javascript/jQuery 并 try catch 当前正在执行的函数的调用者名称。
因此在下面的示例中,日志将显示 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();
});
});
以上显然行不通,因此我向大家提问。
有没有什么好的方法可以获取上面例子中的caller?
请注意:我知道我可以得到 runMe
的被调用者并将其作为参数传递给 showMe
但这个问题的目标是解决方案不需要将调用方“手动”传递到函数中。
是否有理由反对这样做?