Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  119] [ 7]  / answers: 1 / hits: 29767  / 13 Years ago, thu, april 21, 2011, 12:00:00

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)
console.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?


More From » jquery

 Answers
11

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).


[#92617] Tuesday, April 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;