Contact and difference between call and apply before looking at the information to understand some, last night sleep awake suddenly thought of this problem, found that their connection and the difference between understanding is still very vague. It seems that the lack of collation, knowledge is not coherent. Reflection one or two, the details are as follows:
1 Effects (Contact)
Both methods can hijack another object's method, inheriting another object's properties. is to borrow other people's ways to do their own things.
Functionname.call (this, ARG1,ARG2,ARG3) ==functionname.apply (this, arguments) ==this.functionname (arg1, arg2, Arg3)
2 Usage (difference)
2.1 Usage and differences
Also see Functionname.call (this, ARG1,ARG2,ARG3) ==functionname.apply (this, arguments) ==this.functionname (arg1, Arg2, ARG3);
It can be seen that their role is exactly the same, except that the parameters are transmitted in different forms.
Call belongs to the character of Liu Bei, embarrassed to forget its calm calm----like a passing parameter to borrow the method to do parameters (), and apply is Zhang Fei's character, urgent temper, do things slowly not----a one of the arguments really worry people ~ Well, I want to put the parameters in an array, Wouldn't it be quick to pass an array directly?
2.2 Scenario Reproduction:
Under what circumstances do you use apply and when do you use call?
In the case of an object parameter, it is best to use apply if the parameter is in the form of an array, but Eg:Person.call (This,age,name,grde) If we encounter something similar to pass some different attributes. Then using call to send a parameter is definitely the first choice, because the person needs not an array.
2.3 Skills in Apply
After reading 2.2 people will certainly think of a problem, if we need a non-array parameter, but the use of apply to pass an array, so what will happen?
A: Zhang Fei Cuzhongyouxi, according to the need, the array is parsed into a single parameter.
This stunt, if converted in a normal way, will have to tip Kung Fu, so,apply can be effective array conversion ~
Eg:var max=math.max.apply (null,arguments), which is suitable for the smallest number in an array
It is said that the programmer's simple code than any language has the power to say, do not know whether it is true:
1<! DOCTYPE html>23"en">4<meta charset="UTF-8">5<title></title>6<script>7 function Print (a,b,c,d) {8Alert (a+b+c+d);9 }Ten function Test (a,b,c,d) { OnePrint.call ( This, a,b,c,d); APrint.apply ( This, arguments); -Print.apply ( This, [a,b,c,d]); - } the varArr=[e,b,c,d]; -Test"Cloud in","Sky","Water in","Bottle"); - //when the parameter is clear, call is available, when ambiguous, use apply to combine arguments -Print.call (window,"Cloud in","Sky","Water in","Bottle"); + -</script> + A<body> at -</body> -
This article is my own understanding of writing, the front-end new one, between the lines are inevitably biased, welcome correction ~
Talking about the contact & difference between call and apply