call()
The call() method calls a function with a specified this value and one or more arguments given separately. Grammar:
function.call(thisArg, arg1, arg2, ...)
Copy the code
Return value: The return value of the function called with the this value and arguments provided by the caller. If the method returns no value, undefined is returned.
function fn(){
console.log(this)}Copy the code
Fn () // this is window fn. Call (1) //Number{1},this refers to Number fn. Call (ture) //Boolean{ture},this is boolCopy the code
apply()
The apply() method calls a function with a given this value and the arguments supplied as an array (or array-like object).
Grammar:
function.apply(thisArg, [argsArray])
Copy the code
Return value: The result of calling a function with the specified this value and argument.
functionFn (a,b){reutrn a+b} fn. Apply (undefined,[1,2]) // applies only arraysCopy the code
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.
Grammar:
function.bind(thisArg[, arg1[, arg2[, ...]]])
Copy the code
Return value: Returns a copy of the original function with the specified this value and initial arguments.
// Again, bind does not execute immediately, it copies the original function, creates a new function,
function fn(a,b){
return a+b
}
letX = fn.bind(undefined,1,2) x() //3Copy the code