this - jellyfish-tom/TIL GitHub Wiki

[SOURCES]

In most cases, the value of this is determined by how a function is called.

1. Global Context

In the global execution context (outside of any function), this refers to the global object whether in strict mode or not. In web browsers, the window object is also the global object.

2. Function context

Inside a function, the value of this depends on how the function is called.

- Simple Call

A) Not in strict mode

When code is not in strict mode, and when the value of this is not set by the call, this will default to the global object, which is window in a browser.

B) Strict mode

In strict mode, however, the value of this remains at whatever it was set to when entering the execution context, so, in same (as in A) case, this will default to undefined

- As an object method

When a function is called as a method of an object, its this is set to the object the method is called on.

To pass the value of this from one context to another, use one of call vs apply vs bind vs arrow function