about 7 years ago
function test() {
  console.log(this);    // window

  function inner(){
    console.log(this);  // window

  }
  inner();
}

The this keyword inside the inner() function also refers to the Window object. This is because:

When a function is invoked as a function rather than as a method, the this keyword refers to the global object. Confusingly, this is true even when a nested function is invoked (as a function) within a containing method that was invoked as a method: the this keyword has one value in the containing function but (counterintuitively) refers to the global object within the body of the nested function.

← JavaScript for...in vs Object.keys() vs Object.getOwnPropertyNames JavaScript bind, call and apply →
 
comments powered by Disqus