Apply, call exists to change the direction of this inside of the function body and change the scope of the function.
The function People () {}
People. The prototype = {
Age = 18,
SayAge = function () {
Return this. The age
}
Let me = new People()
Me. SayAge () / / 18
Let you = {age: 22}
Me. SayAge. Apply (you) / / 22
Me. SayAge. Call (you) / / 22
You can see that apply, the function of call is the same, all of them change this direction dynamically, but they accept parameters differently
Apply (this, (arg1, arg2, arg3))
Apply the parameter as an array
Call (this, arg1, arg2, arg3)
The call parameters are passed in order
Bind the explanation was the MDN: bind () method to create a new function, when invoked, the this keyword is set to provide value, when a new function called, before any offer provide the parameter of a given sequence.
Fun. Bind (thisArg [, arg1, arg2 [[,...]]])
When the binding function is called, this parameter will be used as this point when the original function is running. When the binding function is called with the new operator, the parameter is invalid.
Arg1, arg2... When the binding function is invoked, the parameters are passed to the bound method before being placed in the argument
Apply, call is an immediate function, and bind is when you change the context and not execute immediately, but when the callback is executed.
Knock on the blackboard and sum up
Apply, call, bind are all points to which this object is used to change the function
The first parameter of apply, call, bind is the object to be pointed to, which is to specify the top or bottom
wen
Apply, call, bind all can use the following parameter to pass the reference
Bind is to return the corresponding function for later invocation; Apply, call, is an immediate call
Today, xiao shifu took a class and asked for the maximum minimum value of the array
The es6 solution math.max (... [1,2,3] // 3 use... Array deconstruction
Math.max. Apply (null, [1,2,3]) // 3 is magic
Math. Max () [1, 2, 3] / / NaN