Everybody seems to explain the ‘this’ keyword differently and many of the explanations seem to conflict with each other and all of them only partially and superficially explain it. The only unifying principle that ‘this’ is founded on seems to be the ‘execution context’ which I’m going to investigate tomorrow. For now though, this is the ‘rote’ understanding I’ve pieced together. Is the following correct?
-
’this’ can never be a callable object(a function or method), so it will only ever be a normal object or an array.
-
’this’ always refers to the default global object until you place it within the code block of a normally written and named method or function, at which point ‘this’ will search upward in scope for the first non-callable object to refer to. No matter how deeply I nest objects within objects, or arrays within arrays, or functions within functions, any references to ‘this’, unless placed within a function which is contained within and object/array, will retain the default value of the global object.
-
within arrow functions, ‘this’ is always the default global object unless you force it to be something else.
-
within anonymous functions, ‘this’ always refers to the default global object unless you force it to be something else via bind/apply/call or passing it from a higher scope as an argument.
-
When in strict mode, ‘this’ is undefined unless within the code block of a named and normally written function/method or forced to be something else via bind/apply/call or passing it from a higher scope as an argument or assigning it a value from any given argument.