bind, apply, call the difference between call () and appl () understanding

Changing this pointer to a function of the internal functions:

By changing the function and apply call this point, their first two parameters are the same function pledged to change that object pointed to by the second argument, apply an array, and the call is arg1, arg2 ... this form. By changing this scope will bind returns a new function that will not be executed immediately.

 

call () and Appl () is understood

Each function contains two methods of non-inherited: apply () and call ()

1. The use of these two methods: calling a function specific domain effect, substantially equal to the set value of this object function in vivo

(1) apply () method takes two parameters: a scope in which the function is running, there is an array of arguments (Array or arguments object instance)

function sum(num1,num2){

return num1 + num2

}

function callSum(num1,num2){

return sum.apply(this,arguments);

}

alert(callSum(10,10));//20

callSum () passing in the implementation of sum () function of this as this value (because it is a global call, all incoming is the window object), and the arguments object

(2) call () method with the same function apply (), except that different ways acceptable parameters, the first parameter is Call this value does not change, change in the remaining parameters passed to the function directly.

function sum(num1,num2){

return num1 + num2

}

function callSum(num1,num2){

return sum.call(this,num1,num2);

}

alert(callSum(10,10));//20

2.apply () and call () is a real strong place to be able to expand the scope of functions on which to run.

window.color = "red"

var o = {color:"blue"};

function sayColor(){

alert(this.color);

}

sayColor(); //red

sayColor.call(this);//red

sayColor.call(window);//red

sayColor.call (o); // blue (even function which until then added sayColor var color = "red", the result is still blue, because the body of the function sayName this point has a o)

He explained: sayColor () is defined as a global function, when you call it in the global domain, it will show red; sayColor.call (this) in this is wimdow, and sayColor.call (window), are in a global call sayColor scope (), the result will still display 'red',

However, when running sayColor.call (o), the function execution environment is not the same, because the body of the function of this object point O, the result is a blue

Use call () and apply to expand the scope of greatest benefit is that there need not be any relationship between the coupling method

Each function contains two methods of non-inherited: apply () and call ()

1. The use of these two methods: calling a function specific domain effect, substantially equal to the set value of this object function in vivo

(1) apply () method takes two parameters: a scope in which the function is running, there is an array of arguments (Array or arguments object instance)

function sum(num1,num2){

return num1 + num2

}

function callSum(num1,num2){

return sum.apply(this,arguments);

}

alert(callSum(10,10));//20

callSum () passing in the implementation of sum () function of this as this value (because it is a global call, all incoming is the window object), and the arguments object

(2) call () method with the same function apply (), except that different ways acceptable parameters, the first parameter is Call this value does not change, change in the remaining parameters passed to the function directly.

function sum(num1,num2){

return num1 + num2

}

function callSum(num1,num2){

return sum.call(this,num1,num2);

}

alert(callSum(10,10));//20

2.apply () and call () is a real strong place to be able to expand the scope of functions on which to run.

window.color = "red"

var o = {color:"blue"};

function sayColor(){

alert(this.color);

}

sayColor(); //red

sayColor.call(this);//red

sayColor.call(window);//red

sayColor.call (o); // blue (even function which until then added sayColor var color = "red", the result is still blue, because the body of the function sayName this point has a o)

He explained: sayColor () is defined as a global function, when you call it in the global domain, it will show red; sayColor.call (this) in this is wimdow, and sayColor.call (window), are in a global call sayColor scope (), the result will still display 'red',

However, when running sayColor.call (o), the function execution environment is not the same, because the body of the function of this object point O, the result is a blue

Use call () and apply to expand the scope of greatest benefit is that there need not be any relationship between the coupling method

Guess you like

Origin www.cnblogs.com/psxiao/p/11545936.html