Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the caller context in javascript?

Tags:

javascript

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

like image 592
new_perl Avatar asked Mar 13 '12 05:03

new_perl


People also ask

What is JavaScript caller function?

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.

What is a function call context?

The "calling context" refers to the context which the native eval function is being called from.

What is context in JavaScript?

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.

Which command gives a value to the function caller in JavaScript?

You can use Function. Caller to get the calling function.


1 Answers

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.

like image 183
otakustay Avatar answered Sep 21 '22 19:09

otakustay