0

Title says it all.

I am so confused about whole concept of execution context in JavaScript. I understand that each execution context is associated with one variable object, and variable object stores declared variables, functions and formal parameters.

The word "execution context" is so abstract term for me to understand. If the variable object stores everything, then what is this word "execution context" for? Are these just two word for same thing?

ringord
  • 908
  • 3
  • 11
  • 27
  • 3
    [This question has some information you may find helpful.](http://stackoverflow.com/questions/9384758/what-is-the-execution-context-in-javascript-exactly) – Pointy Mar 06 '15 at 16:16

1 Answers1

0

No, they're separate things.

All the gory details are in the spec in §10.4.3 and the sections it links to (particularly §10.5), but fundamentally an execution context has a variable binding object, but it has other things as well, like a reference to its containing context (which is what gives us the scope chain) and the value of this within the context.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I see, That's helpful. Thanks. And, in case of Web Browser, `window` object is a variable binding object for the global context, and all the variables, functions , etc are stored in Window Object(this was the part that this question arose from). Am i right? – ringord Mar 06 '15 at 16:50
  • @beli: The global object is the variable binding object for the global context, yes ([§10.4.1](http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.1) and 10.4.1.1) -- on browsers and not on browsers. `window`, on browsers, is a predefined global variable (that is, a property on the global object) which has a reference to the global object. Non-browser environments don't usually have an equivalent, but you can create one because `this` at global scope is also a reference to the global object. So `this.global = this;` (at global scope) would create an equivalent global called `global`. – T.J. Crowder Mar 06 '15 at 16:58
  • One more question, I read [$10.3](http://www.ecma-international.org/ecma-262/5.1/#sec-10.3). Are all three components of Execution Context stored as `Object` Type? Can we call them Object(e.g variable object, lexical environment object, thisbinding object)? – ringord Mar 06 '15 at 17:24
  • @beli: They're *conceptual* objects in terms of the specification, FWIW calling them "objects" seems perfectly reasonable to me, I don't think anyone would be surprised by your doing that. This is one of the reasons I say JS is object-oriented "all the way down." And of course, they may be **literal** objects in an implementation (but we have no way of knowing, because we can't directly access them). They would probably *not* literally be the result of `{}` or `new Object()` (in that they probably don't have `Object.prototype` as their prototype). – T.J. Crowder Mar 06 '15 at 17:31
  • Thanks, T.J. This helped alot. – ringord Mar 06 '15 at 17:33
  • We can access to `thisbinding component` with `this` keyword, though? – ringord Mar 06 '15 at 17:36
  • @beli: Yup, just not the execution context, lexical environment, or variable environment. I don't think I've ever wanted direct access to the execution context, but I've absolutely wanted direct access to the lexical/variable environment objects. :-) Maybe someday, although that would make it hard on JS engine optimizers. – T.J. Crowder Mar 06 '15 at 17:37