In layman's words, call and apply allow a function to invoke the properties and methods of other functions, just passing in the parameters differently.
1. Use apply to the method of the other function, the passed parameter is a collection of all parameters, such as the following code
function aaa (name,sex,height) { bbb.apply (This, arguments); at This point the AAA,BBB is the same, the BBB will output ' 170cm '}; // call function, pass back the parameter aaa (' Zhang San ', ' Male ', ' 170cm ') ; function BBB (name,sex,height) { alert (height);};
To output the parameter is height, you need to change the BBB into the parameter
2. Use the call method to call the method of other functions, the parameters passed in is a parameter, such as the following code
function aaa (name, sex) { Bbb.call (this// ) The parameters returned by the call method are objects, directly selecting the parameters that need to be returned;}; // call function, pass back the parameter aaa (' Zhang San ', ' Male ', ' 170cm '); function bbb (height) { alert (height);};
Summary: The difference between the two is that the form of the Apply return parameter is a parameter group, and the call method returns the parameter as a single Parameter object.
The relationship between call and apply and shallow solutions