Is there a way to get the name of the caller function within the callee? [duplicate]
Possible Duplicate:
Javascript how do you find the caller function?
I'm experimenting with javascript/jQuery a bit this morning and was trying to capture the caller name of the currently executing function.
So in the below example, the log would show runMe
as the caller and showMe
as the callee.
jQuery(document).ready(function($) {
function showMe() {
// should log the runMe as the caller and showMe as callee
console.log('Callee: ',arguments.callee)
conso开发者_C百科le.log('Caller: ',arguments.caller);
}
function runMe() {
// getting executed as a button is pressed.
showMe();
}
$('a').bind('click', function(e) {
e.preventDefault();
runMe();
});
});
The above obviously doesn't work, hence my question to you all.
Is there a good way to get the caller in the above example?
please note: I am aware I could get the callee of runMe
and pass it into showMe
as an argument but this question is aiming towards a solution that does not require the caller to be passed into the function 'manually'.
Are there reasons against doing something like this?
You used to be able to do arguments.caller.name
, but this is deprecated in Javascript 1.3.
arguments.callee.caller.name
(or just showMe.caller.name
) is another way to go. This is non-standard, and not supported in strict mode, but otherwise currently supported in all major browsers (ref).
Try callee.caller
like this
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);
}
Does this work for you?
function showMe() {
// should log the runMe as the caller and showMe as callee
console.log('Callee: ',arguments.callee)
console.log('Caller: ',arguments.callee.caller);
}
Note, this is non-standard javascript.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/caller
I think it's....
arguments.callee.caller
精彩评论