Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function context ("this") in nested functions

Tags:

javascript

When you invoke a top-level function in Javascript, the this keyword inside the function refers to the default object (window if in a browser). My understanding is that it's a special case of invoking the function as method, because by default it is invoked on window (as explained in John Resig's book, Secrets of the JavaScript Ninja, page 49). And indeed both invocations in the following code are identical.

function func() {
  return this;
}

// invoke as a top-level function
console.log(func() === window); // true

// invoke as a method of window
console.log(window.func() === window); // true

So far so good... Now here is the part I don't understand:

When a function is nested in another function and invoked without specifying an object to invoke on, the this keyword inside the function also refers to window. But the inner function cannot be invoked on window (see code below).

function outerFunc() {
  function innerFunc() {
    return this;
  }

  // invoke without window.* - OK
  console.log(innerFunc() === window); // true

  // invoke on window
  //window.innerFunc(); - error (window has no such method)
  console.log(window.innerFunc) // undefined
}

outerFunc();

It makes perfect sense that the nested function isn't available on window, as it is after all nested... But then I don't understand why the this keyword refers to window, as if the function was invoked on window. What am I missing here?

EDIT

Here is a summary of the great answers below and some of my follow up research.

  • It is incorrect to say that invoking a function "normally" is the same as invoking it as a method of window. This is only correct if the function is defined globally.

  • The function context (the value of the this keyword) does not depend on where / how the function is defined, but on how it is being invoked.

  • Assuming that the code is not running in in strict mode, Invoking a function "normally" will have the function context set to to window (when running in a browser, or the corresponding global object in other environments).

  • An exception to the above rules is the use of bind to create a function. In this case even if the function is invoked "normally", it could have a context other than window. That is, in this case the context is determined by how you create the function, rather than how you invoke it. Although strictly speaking this isn't accurate, because bind creates a new function that internally invokes the given function using apply. The context of that new function will still be determined by the way it's invoked, but it shields the context of the function it internally invokes by using apply.

By invoking "normally" I refer to the following simple way of invocation:

myFunction();

To complete the picture, here is a brief coverage of other ways of invocation and the corresponding context:

  • As a property of an object (method) - the context is the object

  • Using apply or call - the context is specified explicitly

  • With the new operator (as a constructor) - the context is a newly created object

Feel free to update the above as necessary, for the benefit of people with similar questions. Thanks!

like image 395
M.S Avatar asked Feb 03 '13 21:02

M.S


1 Answers

You can call any function that is in scope with functionName(). Since you haven't called it on an object, it will be called in the context of the default object (window). (IIRC, it will be called in the context of undefined if you are in strict mode).

The default object for context has nothing to do with where a function is defined or what scope that function appears in. It is simply the default object.

If a function is a property of an object, you can call it as reference.to.object.function(), and it will be called in the context of object instead of the default object.

Other things that change the context are the new keyword and the apply, call, and bind methods.

like image 147
Quentin Avatar answered Sep 22 '22 11:09

Quentin