0

In the code below:

var x = {
    y: function() {
        console.log(this === global); // false
        function z() {
            console.log(this === global); // true
        };
        z();
    }
};

x.y();

When you call y, you are providing the execution context of x. As such this is the x object.

Why is the execution context of z the global object (or window object in a browser)?

It seems like it would be impossible to create a different execution context for z in the way it is defined. i.e. this.z(), global.z(), x.z(), y.z() all error with the message that there is no z function in that execution context.

For this reason, is it true to say that functions defined in the form function name() {} are ALWAYS defined in the global object's execution context? AND that unless call/bind/apply are used to invoke the function, execution is ALSO ALWAYS in the global object context?

Question: why does a function in this form function name() ... get defined in the global context and not the context of the object it is written in?


The above is in response to this article: http://www.digital-web.com/articles/scope_in_javascript/

"What if we just call a normal, everyday function...

 <script type="text/javascript"> 
  function test_this() { 
   return this; 
  } 
  var i_wonder_what_this_is = test_this(); 
 </script>

In this case, we weren’t provided a context by new, nor were we given a context in the form of an object to piggyback off of. Here, this defaults to reference the most global thing it can: for web pages, this is the window object."

Zach Smith
  • 8,458
  • 13
  • 59
  • 133
  • where do you get `global` from? – Nina Scholz Jun 10 '16 at 06:40
  • What is a "call site"? You might want to read [MDN's article about how `this` works in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this). – nnnnnn Jun 10 '16 at 06:41
  • `this` depends on *how you call the function.* The difference is precisely in `x.y` vs. just `z`. – deceze Jun 10 '16 at 06:43
  • @deceze I have edited the question, the intention was not to ask about the `this` object, but how `function name() ...` definitions are treated – Zach Smith Jun 10 '16 at 07:24
  • 1
    Functions aren't "defined" within any context (only *literal scope*, but that's a slightly different topic). The context, i.e. what `this` refers to, is decided at call time and purely by *how you call* the function. So, yes, a `function foo` can only be called as `foo()`, so will by default have the global object as `this`. That is, unless you reassign it; e.g. `this.foo = foo; this.foo()`... **Call time** matters, not declaration. – deceze Jun 10 '16 at 07:32
  • Thanks. So I guess my question is where can I find out more about how `function name()...` declarations are actually handled. Is this a question regarding how the Javascript engine works? – Zach Smith Jun 10 '16 at 07:34
  • Except for hoisting behaviour, `function foo ..` is exactly equivalent to `var foo = function ..`. There's nothing else special about it. – deceze Jun 10 '16 at 07:37

0 Answers0