0

I am aware of the fact in the global context this refers to the window object meaning

function outer() {
   console.log(this === window);
}

window.outer(); // true;
outer(); // true

Since, the function outer becomes a property of the window object.

But, when it is in the form of an inner function such as this :

const obj = {
    method: function() {
      function inner() {
        console.log(this === window);
      }

      window.inner(); // TypeError: window.inner is not a function
      inner(); // true
    }
};

obj.method();

Going by the fact that it depends on the execution context to figure out this when calling a function. So, if this === window in this case which it is, shouldn't window.inner() work too? I am a bit confused here ...

  • "`So, if this === window in this case which it is`" .. is it? Try logging it right before your call to `window.inner()`. – TiiJ7 Jun 08 '20 at 15:39
  • I think you'll be surprised to find that `this` is actually the `method` function rather than the `window` object – Red Baron Jun 08 '20 at 15:39
  • Yes, `this === window` itself, reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this I found my answer which says if value of `this` is not explicitly set by the call, it defaults to the `window` object – Gagan Ganapathy Ajjikuttira Jun 08 '20 at 15:40

0 Answers0