Overview
To know what ‘this’ points to, you have to know how the related function is called in JavaScript
The function’s this
is bound when called, depending entirely on where the function is called (that is, how the function is called). To know what this
points to, you have to know how the related function is called.
Global context
In both non-strict and strict modes this
refers to the top-level object (window
in the browser).
|
|
Function context
|
|
You might mistakenly think window.doSth()
is called and therefore points to window. Although in this case window.doSth
does equal doSth
. name
equals window.name
. This is because in ES5, global variables are mounted in the top-level object (browser is Window).
In fact, it’s not.
|
|
In this example, let
does not add attributes to the top-level object (the browser is Window
); window.name2
and window.doSth2
are both undefined
.
In strict mode, this
in normal functions behaves differently, as undefined
.
|
|
This is called default binding. Readers familiar with call
and apply
will use the analogy { 类比;类推;比喻 }:
|
|
The effect is the same. One of the things that call
apply
does is to change what this
refers to as the first parameter in a function. The first argument is undefined or null, which in non-strict mode points to window. In strict mode, it refers to the first parameter. The following article explains in detail.
There is often code like this (the callback function), which is actually a normal function call pattern.
|
|
Object function (method) invocation pattern
|
|
However, there are often situations where a function in an object is assigned to a variable. This is actually a normal function again, so use the rules of normal functions (default binding).
|
|
call( ), apply( ), bind( )
Above mentioned call
, apply
, here is a detailed interpretation. Function.prototype.call() , Check out my other article on how they are implemented
|
|
According to the description of the parameter thisArg
, it can be known that call
is to change the this in the function to point to thisArg
and execute the function, which makes JS
much more flexible. In strict mode, thisArg is a primitive value that is a value type, that is, a primitive value. will not be wrapped into an object. for example:
|
|
Constructor call pattern
|
|
From this, we can know that when the new operator is called, this points to the new object generated.
Calling Patterns in the Prototype Chain
|
|
Will find this familiar. This is the method invocation pattern on an object. Naturally, it points to the new object generated. If the object inherits from other objects. It will also look up through the prototype chain.
Arrow function call pattern
Let’s first look at the important differences between arrow functions and ordinary functions:
- It does not have its own
this
,super
,arguments
andnew.target
bindings. - You cannot use new to
call
. - There is no
prototype
object. - The binding of
this
cannot be changed. - The formal parameter { 形参 } name cannot be repeated.
There is no this binding
in an arrow function, its value must be determined by looking up the scope chain. If the arrow function is contained by a non-arrow function, this is bound to the this of the nearest non-arrow function, otherwise the value of this is set to the global object. for example:
|
|
In fact, it is equivalent to this outside the arrow function, which is the cached this
of the ordinary function above the arrow function. If there is no normal function, it is the global object (window
in browsers).
That is to say { 也就是说 }, it is impossible to bind the this of the arrow function through call, apply, and bind (it itself does not have this). And call
, apply
, bind
can bind
the this
of the ordinary function above the cache arrow function. For example:
|
|
DOM event handler function call
|
|
In onclick
and addEventListener
, this points to the elements of the bound event.
In Summary
If you want to determine the this binding of a running function, you need to find the direct call location of the function. After finding it, you can apply the following four rules in order to determine the binding object of this
.
new
call: bind to the newly created object, note: display the return function or object, the return value is not the newly created object, but the explicitly returned function or object.call
orapply
(orbind
) call: In strict mode, bind to the specified first parameter. In non-strict mode,null
andundefined
point to the global object (window in browsers), and the rest of the values point to objects wrapped bynew Object().
- A function call on an object: bind to that object.
- Ordinary function calls: bind to
undefined
in strict mode, otherwise bind to the global object.
Arrow functions in ES6
: The above four standard binding rules will not be used, but this
will be determined according to the current lexical scope.
Specifically, arrow functions will inherit the outer function and call this
binding (regardless of what this
is bound to), if there is no outer function, it is bound to the global object (window
in the browser).
DOM
event function: generally points to the DOM
element to which the event is bound, but in some cases it is bound to the global object.