The similarities and differences between apply () and call () in JavaScript are as follows: javascriptapply
Each function contains two non-inherited methods: apply () and call (). The purpose of these two methods is to call a function in a specific scope, which is actually equal to setting the value of this object in the function body.
The apply () method receives two parameters: one is the scope in which the application runs, and the other is the parameter array. The second parameter can be an Array instance or an arguments object. For example:
Function sum (num1, num2 ){
Return num1 + num2;
}
Function callSum1 (num1, num2 ){
Return sum. apply (this, arguments); // input the arguments object
}
Function callSum2 (num1, num2 ){
Return sum. apply (this, [num1, num2]); // input an array
}
Alert (callSum1 (10, 10); // 20
Alert (callSum2 (10, 10); // 20
The call () method and the apply () method have the same effect. The difference is that the method of receiving parameters is different. For the call () method, the first parameter is that the value of this has not changed, and all other parameters are directly transferred to the function. In other words, when using the call () method, the parameters passed to the function must be listed one by one. For example:
Function sum (num1, num2 ){
Return num1 + num2;
}
Function callSum (num1, num2 ){
Return sum. call (this, num1, num2 );
}
Alert (callSum (10, 10); // 20
When the call () method is used, callSum () must explicitly input each parameter. The result is no different from apply. The use of apply () or call () depends entirely on which method you use to pass parameters to the function for convenience.
The above summary is taken from JavaScript advanced programming.