Apply () and call ()
Although in a separate function call, depending on whether it is strict mode, this point points to undefined or window, however, we can still control the point of this!
To specify which object this function's this point points to, you can use the Apply method of the function itself, which receives two arguments, the first argument in which the function's scope is run, the second argument is an instance of the array, or it can be an arguments object.
Another method similar to apply () is call (), the only difference being: Apply () packages the parameters into an array and then incoming, call () passes the parameters in order. For example, Invoke Math.max (3, 5, 4), respectively, with apply () and call () implemented as follows: Math.max.apply (null, [3, 5, 4]); Math.max.call (NULL, 3, 5, 4); For normal function calls, we usually bind this to null.
function sum (NUM1, num2) { return num1 + num2;} function Callsum (NUM1, num2) { return Sum.call (this, NUM1, num2);} function CallSum1 (NUM1, num2) { return sum.apply (this, arguments);} function callSum2 (NUM1, num2) { return sum.apply (this, [Num1, num2]);} Console.log (Callsum (10,10)); 20console.log (CALLSUM1 (10,10));//20console.log (CallSum2 (10,10));//20
Apply and call are often used to extend the scope in which functions are run.
Window.color = ' red '; var o = {color: ' Blue '};function saycolor () { console.log (this.color);} Saycolor (); Redsaycolor.call (this); Redsaycolor.call (window); Redsaycolor.call (o); Blue
In the above example, a global function saycolor is defined, and when called in the global scope, the evaluation of This.color is converted to the evaluation of Window.color, Saycolor.call (this) and Saycolor.call ( window) is the way that two displays are called in the global scope, and the result is "red", but when you run Saycolor.call (o), the function's execution environment is different, and the this object in the function points to O, and the result is "blue."
The biggest benefit of using call () and apply () to extend the scope is that the object does not need to have any coupling with the method.
function Getage () {var y = new Date (). getFullYear (); return Y-this.birth;} var xiaoming = { name: ' Xiaoming ', birth:1990, age:getage};console.log (Xiaoming.age ()); 27console.log (getage.apply (xiaoming,[)); 27
Bind ()
Bind () is a new method of ES5, and the main function of this method is to bind a function to an object
When you call the Bind () method on the function f () and pass in an object o as a parameter, this method returns a new function. Calling the new function as a function call will call the original function f () as O, and any arguments passed into the new function will be passed to the original function.
function f (y) {
The code above is called by the original function f () as the O method, and the incoming arguments are passed into the original function.
var o={ x:1 f:function (y) { return this.x + y; Y=2 }}
The bind () method not only binds a function to an object, it also comes with some other applications: the arguments to the incoming bind () are bound to this, in addition to the first argument, and this accompanying application is a common functional programming technique, sometimes referred to as ' curry ' (currying).
var sum = function (x, y) { return x+y;} var succ = Sum.bind (null,1); The bind () method is raised on the function sum and passed in an object window, a new function succ is obtained, and an argument is reached to the object Succ (2); 3,x binds to 1 and passes in 2 as argument yfunction F (y,z) { return this.x + y + z;//this points to the currently called object or Window}var g = F.bind ({x:1},2); G (3);//6,t His.x bound to 1,y bound to 2,z bound to 3
Apply, call, and bind