ECMASCRIPT3 defines two methods on a function prototype, which are Function.prototype.call and Function.prototype.apply.
- The difference between call and apply
1 varFunc =function(a,b,c) {2 alert ([A,b,c]);3 };4Func.apply (NULL, [+]);//Apply5 6 7 varFunc =function(a,b,c) {8 alert ([A,b,c]);9 };TenFunc.call (NULL, and A.);//Pager
Apply:: Two parameters, first parameter: Specifies the pointer to this object in the function body;
Second parameter: An indexed collection, either an array or a pseudo-array
Call: Multiple parameters: The first parameter: Specifies the pointer to this object in the function body;
The second parameter begins, and each parameter is passed to the function sequentially;
The first argument passed by call and apply is NULL, and this in the function body points to the default host object, which in the browser is the window
If in strict mode, the this in the function body is still null 1 var func = function(a,b,c) {
2Alert This= = = window);//true3 };4 5Func.apply (NULL, [A])6 7 varFunc =function(a,b,c) {8"Use Stract";//If this is still null in the body of the function in strict mode9Alert This===NULL);//trueTen }; OneFunc.apply (NULL, [+]);
1. Change the direction of this
1 varObj1 = {2Name: "Seven"3 };4 5 varOj2 = {6Name: "Anne"7 };8 9window.name = "Window";Ten One varGetName =function(){ AAlert This. Name) - }; - theGetName ();//window -Getname.call (OBJ1);//Seven -Getname.apply (OBJ2);//AnneView Code
In the actual development, often encounter this point is inadvertently changed scenes, for example, there is a div node, the div node's onclick event in the This will be pointed to this div:
1Dcument.getelementbyid ("Div1"). onclick=function(){2Alert This. ID);//Div13 }4 //Adding the change time function has an internal function func, when invoking the Func function inside the Func function, this is pointing to the window instead of the div we expect.5 6document.getElementById ("Div1"). Oonclick =function(){7Alert This. ID);//Div18 varFunc =function(){9Alert This. ID)//undefinedTen }; One func (); A }; - //at this point we use call to fix this in the Func function, which points to the Div -document.getElementById ("Div1"). onclick=function(){ the varFunc =function(){ -Alert This. ID);//Div1 - }; -Func.call ( This); +}
Using call to fix this scenario is not the first time we have encountered it,
1document.getElementById = (function(){2 return function(){3 returnfunc.apply (document,arguments);4 }5 }) (document.getElementById);6 7 varGetId =document.getElementById;8 vardiv = getId ("Div1");9alert (div.id);//Div1
2.function.prototype.bind
Most browsers implement a built-in Function.prototype.bind that specifies the this point within the function, even if there is no native Function.prototype.bind implementation.
1Function.prototype.bind=function(context) {2 varSelf = This;//Save original function3 return function(){//returns a new function4 returnSelf.apply (context,arguments);//when a new function is executed, the previous context is used as this in the new function body.5 };6 }7 8 varobj = {9Name: "Seve"Ten }; One A varA =function(){ -Alert This. name);//Seve - }.bind (obj); the -A ();
We "wrap" the Func function through Function.prototype.bind, and pass in an object context as a parameter, which is the this object we want to fix.
In the internal implementation of Function.prototype.bind, we first save the reference to the Func function and then return a new function. When we execute a function in the future, we are actually going to execute the new function that we just returned. Inside the new function, Self.apply (context,arguments) is the code that executes the original a function, and specifies that the context object is this in the body of the a function.
The following is a more complex implementation:
JS Call and apply