- Use the arrow function of ES6
- Assign this inside the function
- Use the apply, call, bind methods
- New instantiates an object
The arrow function changes this
The arrow function itself does not have this property. The this used inside the arrow function refers to the this at the time the function was defined. There is no this binding in the arrow function, and its value must be determined by searching the scope chain. This is undefined
Assign this inside the function
Store the value of the object calling the function (that is, this) in a variable, such as _this or that, where appropriate, and use _this in the function. In this case, _this will not change
Use apply, call, and bind
In strict mode, the first argument is referred to as this inside the function. In non-strict mode, if the first argument is null or undefined, this points to global. This with a primitive value (Number, String, Boolean) points to an auto-wrapped object for that primitive value, such as String, Number, Boolean
Function.prototype.bind()
The bind() method creates a new function, and when bind() is called, this of the new function is specified as the first argument to bind(), and the remaining arguments will be used as arguments to the new function.
function.bind(thisArg[, arg1[, arg2[, ...]]])
Copy the code
thisArg
- When a binding function is called
this
The value that the argument passes to the target function. If you are usingnew
Operator to construct a binding function, the value is ignored. When usingbind
在setTimeout
When a function (provided as a callback) is created inthisArg
Any raw values passed will be converted toobject
. ifbind
The argument list of the function is empty, orthisArg
isnull
orundefined
, perform scopedthis
Will be treated as a new functionthisArg
.
The return value
Returns a copy of the original function with the specified this value and initial arguments.
Function.prototype.call()
The call() method calls a function with a specified this value and one or more arguments given separately.
function.call(thisArg, arg1, arg2, ...)
Copy the code
thisArg
- Optional. in
function
Used when the function is runthis
Value. Please note that,this
May not be the actual value that the method sees: if the function is inNonstrict modeUnder, the value isnull
或undefined
Is automatically replaced with a reference to a global object, and the original value is wrapped.
The return value
Call the return value of the function with the this value and arguments provided by the caller (execute the function immediately). If the method returns no value, undefined is returned.
Note: The syntax and actions of this method are similar to those of apply(), except that call() accepts a list of arguments, while apply() accepts an array of arguments.
Function.prototype.apply()
The apply() method calls a function with a given this value and arguments in the form of an array (or array-like object).
func.apply(thisArg , [ argsArray])
Copy the code
thisArg
- Will be selected. in
func
Used when the function is runthis
Value. Please note that,this
May not be the actual value that the method sees: if the function is inNonstrict modeUnder, the value isnull
或undefined
Is automatically replaced with a reference to a global object, and the original value is wrapped.
The difference between call and bind
- The functions of Apply and Call are basically similar, except that the format of the parameters received is different.
- Apply receives an array of arguments, and Call receives a list of arguments.
- The Apply and call methods, when used, change the this reference and then execute the function immediately after passing the parameter, returning the result
- The bind method creates a new function in which the reference to this is changed and takes the arguments passed by bind. The function will not be executed until it is called manually.
New instantiates an object
When new instantiates the constructor, the call method is used to point this inside the object to the instantiated object. The instantiation object’s __proto__ points to the constructor’s prototype, protiType