bind()
is a built-in method of Function
object. Their first parameter is used to change the calling method# Pointing to ##this. It should be noted that
bind returns a new function so that it can be called later.
1. Grammar:
1 |
|
thisArg: used as this when calling the bound function The parameter value passed to the target function. If the new operator is used to construct the bound function, this value is ignored. When using
bind to create a function in setTimeout (provided as a callback), any primitive value passed as
thisArg will be converted to an
object. If the parameter list of the
bind function is empty, or
thisArg is
null or
undefined,
this ## of the execution scope # will be treated as thisArg
of the new function.
arg2,
...: When the target function is called, it is preset into the parameter list of the bound function parameters.
Return value: Returns a copy of the original function and has the specified this Values and Initial parameters.
2. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
3. Output
1 2 3 |
|
The above is the detailed content of A brief discussion on bind() in JS. For more information, please follow other related articles on the PHP Chinese website!