code_junkie

How do you find out the caller function in JavaScript?

Nov 12th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. function main()
  2. {
  3. Hello();
  4. }
  5.  
  6. function Hello()
  7. {
  8. // How do you find out the caller function is 'main'?
  9. }
  10.  
  11. function Hello()
  12. {
  13. alert("caller is " + arguments.callee.caller.toString());
  14. }
  15.  
  16. function Hello() {
  17. alert("caller is " + arguments.callee.caller.toString());
  18. }
  19.  
  20. function Hello() {
  21. alert("caller is " + Hello.caller.toString());
  22. }
  23.  
  24. var Klass = function kls() {
  25. this.Hello = function() { alert(printStackTrace().join('nn')); };
  26. }
  27. new Klass().Hello();
  28.  
  29. arguments.callee.caller.name
  30.  
  31. arguments.callee.caller.toString();
  32.  
  33. arguments.callee.caller
  34. arguments.callee.caller.caller
  35. arguments.callee.caller.caller.caller
  36.  
  37. function Hello() {
  38. alert(Hello.caller);
  39. }
Add Comment
Please, Sign In to add comment