Search code examples
javascriptexecutioncontext

Understanding execution context inside object literal


I am very new to javascript programming, i am trying to understand how javascript program works. Hence i read about concepts like execution context, execution stacks etc. After understanding a bit of execution context i learned that "this" refers to execution context. So i tried to print execution context inside an object literal to check the execution context, hence i wrote the following code.

var obj = {
    method: function() {
        console.log(this);
    }
};
obj.method();

This gives me an output as:

{ method: [Function: method] }

After seeing this i have two questions, that is, is the above code correct to know the execution context?, and if yes, then shouldn't execution context should be an Object { method: function() } instead of the output it is giving.

I tried reading a lot regarding this but i couldn't crack it.


Solution

  • In Chrome the output looks like

    {method: ƒ}
    

    Internet Explorer

    [object Object]   {}
    

    Firefox

    Object { method: method() }
    

    They all mean the same thing and refer to the object obj.