var test = { demo: function(){ //get the caller context here } } //when this gets called, the caller context should be window. test.demo();
I tried arguments.callee
and arguments.callee.caller
,and no luck...
caller property of the JavaScript function object returns the function that invoked the specified function. It returns null if the function “f” was invoked by the top-level code in JavaScript. For functions like strict functions, async functions and generator function this method returns null. Syntax: function.caller.
The "calling context" refers to the context which the native eval function is being called from.
What is context? Context is always the value of the this keyword which is a reference to the object that “owns” the currently executing code or the function where it's looked at. We know that window is a global object in the browser so if we type this in the console and it should return window object, which it does.
You can use Function. Caller to get the calling function.
Since this
keyword referes to ThisBinding
in a LexicalEnvironment
, and javascript (or ECMAScript) doesn't allow programmatic access to LexicalEnvironment
(in fact, no programmatic access to the whole Execution Context
), so it is impossible to get the context of caller.
Also, when you try test.demo()
in a global context, there should be no caller at all, neither an attached context to the caller, this is just a Global Code, not a calling context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With