I have a task that depends upon function.caller to sanity check that a caller is authorized.
According to this url, caller is supported in all major browsers... and all of my unit tests pass:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
However, nodejs rejects all attempts to access function.caller (reports it as null).
I'm open to suggestions to make this work on nodejs... I'd hate to have this framework only work on browsers.
Thanks!
Because function.caller DOES work in nodejs, but fails when combined with Object.defineProperty getters/setters, I'm going to consider this a bug in nodejs, rather than a choice by nodejs not to support function.caller.
You can use arguments.callee.caller to get a reference to the caller. However I believe it's (still) deprecated (although its eventual removal could cause a problem for some cases, see here for that discussion) and you miss out on some compiler/interpreter optimizations too.
Example:
function foo() {
bar();
}
function bar() {
console.log(arguments.callee.caller.name);
}
foo();
// outputs: foo