0

Just trying to clear some misconceptions...

function outer(){
    function inner(){

    }
}
  1. Does a function being a member of a particular object decides it's contextual this ?

  2. Like, is it the reason that outer function is member of window object therefore the contextual this of outer function refers to window object ?

  3. If yes then whos member is the inner function ?

  4. Is inner member of outer function's object ?

  5. And why the inner function's contextual this is referring to the window object ?

  6. Is any function(global or nested) invoked without new operator will have contextual this referring to window object ?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • [How does the "this" keyword work, and when should it be used?](https://stackoverflow.com/q/3127429) – VLAZ Jan 30 '23 at 12:13

1 Answers1

0

For an ordinary function (that is, not an arrow or member function), this is populated dynamically, at the call time. this depends on how the function is being called, not on where it's been declared.

this can be populated by 1) a property accessor (object.func) or 2) new as in new func() or 3) an indirection like call/apply. If the engine cannot populate this for a particular function call, it's set to the global/window in the sloppy mode and undefined in the strict mode. This applies to both global and inner functions.

Global functions being members of the global/window is an orthogonal concept and doesn't affect this in any way.

Inner functions, like local variables, are not members of any object.

gog
  • 10,367
  • 2
  • 24
  • 38