The 1.apply:foo.apply (Obj,args) method is capable of receiving two parameters:
(1) Obj: This object will replace the This object in the function class;
(2) Args: This is an array, which is passed as a parameter to function (args-->arguments);
The most classic of the usages I know about apply is the ingenious implementation of the method of extracting the maximum value of the number array:
function () { return Math.min.apply (nullthis);}; var arr = [1,2,3,4,5,213212,-99];arr.getmin (); -99
2.call:foo.call (obj, args);
Description of the difference between apply and call: The parameter of a for array and C for Comma;call can only be passed one by one;
3. Thebind () method creates a new function, and when the new function is called, its this value is the first parameter passed to bind (), and its arguments are the other parameters of bind () and its original parameters.
Examples of binding functions: reference URL: Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
This. x = 9; varmodule ={x:81, GetX:function() {return This. x;}}; Module.getx (); //BayivarRetrievex =Module.getx;retrievex ();//9, because in this case, ' this ' refers to the global object//Create a new function with ' this ' bound to module//New Programmers (like myself) might confuse the global var GetX with module ' s property GetXvarBoundgetx =Retrievex.bind (module); //<<--------The first parameter of BIND is the object Boundgetx () to which this is pointed; //Bayi
After copying a method under an object, you want this to still point to the module object and get its attribute x value, as in the preceding code: var retrievex = module.getx;
However, when invoking the copied function Retrievex directly, notice that it is called directly within the global scope, then this point of this points to the window, all cannot take out the attribute value under the module object as we "take it for granted";
And bind has the function of changing the scope of the function execution (and apply and call have the same effect), we can use the Bind method when calling, and set the first parameter of bind to the module object, then we change the point of this (point to module), At this point we can achieve the desired goal.
Basic knowledge about. Call,. Bind read it? Do you understand me? Take a study questions to check if you understand these methods.
Q: What is the output of the following code?
Analysis:
1. First the test function outputs the value of the A property of the Action object (this point), so the next idea is to determine the point of this;
2. Three var statements, defines two objects, a binding function.
Object
var obj1= {a:1};
var obj2 = {A:2};
Binding functions
var fn = Test.bind (OBJ2);//bind function within the scope of this point to obj2;
The last key execution statement test.bind (OBJ2). Call (Obj1); the first argument of//call is also the point of this. So the question is, which object does this point refer to?
result : If you answer the output of a is 1 Then it is wrong, because the function obtained by BIND is a closure, and then when called, Only the previously existing value (not this) is found through the closure scope , so using call(which only changes This) cannot change its result. the output is 2.
Learn about. Apply vs. Call and. Bind