Js apply () and call () Methods
- Js apply method details
- At the beginning, I saw the javascript Functions apply and call, which were very vague and hard to understand. Recently, I saw some articles on the Internet showing examples of the apply method and call method, finally, I have a little eye off. Here I will take the following notes and hope to share them with you .. if there is anything wrong or the law is unclear, I hope the readers can give more comments to improve them together ..
-
- I want to solve the following problems:
-
- 1. What is the difference between apply and call?
-
- 2. When to use apply and call
-
- 3. Other clever use of apply (usually when can I use apply)
-
- I first found the definitions of apply and call on the Internet, and then explained the meaning and how to use these two methods using examples.
-
- Apply: the method that can hijack another object and inherit the attributes of another object.
-
- The Function. apply (obj, args) method can receive two parameters.
- Obj: this object will replace the Function classThisObject
- Args: This is an array and it will be passed as a parameter to Function (args --> arguments)
-
- Call: it means the same as apply, except that the parameter list is different.
-
- Function. call (obj, [param1 [, param2 [,... [, ParamN])
- Obj: this object will replace the Function classThisObject
- Params: this is a list of parameters.
-
- 1. apply example:
-
- <Script type = text/javascript>
- /* Define a human */
- FunctionPerson (name, age ){
- This. Name = name;This. Age = age;
- }
- /* Define a student class */
- FunctionStudent (name, age, grade ){
- Person. apply (This, Arguments );This. Grade = grade;
- }
- // Create a student class
- VarStudent =NewStudent (qian, 21, first grade );
- // Test
- Alert (name: + student. name + + age: + student. age ++ grade: + student. grade );
- // You can see the test result name: qian age: 21 grade: grade 1.
- // I didn't assign values to the name and age attributes in the student class. Why are there values of these two attributes? This is the magic of apply.
- </Script>
-
- Analysis: Person. apply (This, Arguments );
-
- This: Student is used to create an object.
-
- Arguments: An array, that is, ["qian", "21", "First Grade"];
-
- In other words, student is used to execute the content in the Person class, which exists in the Person class.ThisIn this way, the attributes are created in the student object.
-
-
-
- 2. call example
-
- In the Studen function, you can change the fix in apply to the following:
-
- Person. call (This, Name, age );
-
- So OK.
-
- 3. When to use apply and call
-
- When an object parameter is given, if the parameter is in the form of an array, for example, the arguments parameter is passed in the apply example, this parameter is of the array type, in addition, when you call Person, the parameter list is consistent (that is, the first two parameters of Person and Student are consistent). You can use apply, if my Person parameter list is like this (age, name), and Student's parameter list is (name, age, grade), you can use call to implement it, that is, directly specify the location (Person. call (This, Age, name, grade ));
-
- 4. Some other clever use of apply
-
- Careful people may have noticed that when I call the apply method, the first parameter is the object (This), The second parameter is an array set,
-
- When calling Person, he does not need an array, but why does he give me an array? I can still parse the array into parameters one by one,
-
- This is a clever use of apply. You can convert an array into a parameter list by default ([param1, param2, param3] To param1, param2, param3) if we want to use a program to implement every item in the array and replace it with a list of parameters, it may take a while. With this feature of apply, therefore, the following efficient methods are available:
-
-
-
- A) Math. max can be used to obtain the largest entry in the array.
-
- Because the Math. max parameter does not support Math. max ([param1, param2]), that is, the array
-
- However, it supports Math. max (param1, param2, param3 ...), So we can solve the problem based on the features of apply.VarMax = Math. max. apply (Null, Array), so that you can easily obtain the largest item in an array.
-
- (Apply will replace a number assembly with a parameter followed by a parameter and pass it to the method)
-
- This is the first parameter in the call.NullThis is because there is no object to call this method. I only need to use this method to help me calculate the result and get the returned result. Therefore,NullPast
-
- B) Math. min can be used to obtain the smallest entry in the array.
-
- Same as maxVarMin = Math. min. apply (Null, Array );
-
- C) Array. prototype. push can merge two arrays.
-
- Similarly, the push method does not provide an array of push, but it provides push (param1, param ,... ParamN) so you can also use apply to replace this array, that is:
-
- Vararr1 =NewArray (1, 2, 3 );
- Vararr2 =NewArray (4,5, 6 );
- Array. prototype. push. apply (arr1, arr2 );
-
- It can also be understood that arr1 calls the push method, and the parameter is a set of parameters that are assembled into parameter lists by applying.
-
- Under what circumstances can I use apply for special usage such as Math. min:
-
- Generally, the target function only needs n parameter lists, instead of receiving an array ([param1 [, param2 [,... [, ParamN]), you can use the apply method to cleverly solve this problem!
-
-
- 5. Conclusion:
-
- At first, I had no idea about apply. I finally read the application several times and I typed the code several times to understand the intermediate principle. Therefore, no matter what you do, as long as you are willing to move your mind and work on the Code, such a technology will master...
-
- For example, the fourth part cleverly solves the problems that actually exist. This is definitely not a solution that beginners can think of (this is not what I think ), if you do not have a certain understanding of programming, you will not think of this. In a single sentence, accumulating and learning more, improving your abilities and understanding of programming ideas are the most critical! From here: http://blog.csdn.net/business122/article/details/8000676
- Js apply method details
- At the beginning, I saw the javascript Functions apply and call, which were very vague and hard to understand. Recently, I saw some articles on the Internet showing examples of the apply method and call method, finally, I have a little eye off. Here I will take the following notes and hope to share them with you .. if there is anything wrong or the law is unclear, I hope the readers can give more comments to improve them together ..
-
- I want to solve the following problems:
-
- 1. What is the difference between apply and call?
-
- 2. When to use apply and call
-
- 3. Other clever use of apply (usually when can I use apply)
-
- I first found the definitions of apply and call on the Internet, and then explained the meaning and how to use these two methods using examples.
-
- Apply: the method that can hijack another object and inherit the attributes of another object.
-
- The Function. apply (obj, args) method can receive two parameters.
- Obj: this object will replace this object in the Function class.
- Args: This is an array and it will be passed as a parameter to Function (args --> arguments)
-
- Call: it means the same as apply, except that the parameter list is different.
-
- Function. call (obj, [param1 [, param2 [,... [, ParamN])
- Obj: this object will replace this object in the Function class.
- Params: this is a list of parameters.
-
- 1. apply example:
-
- <Script type = text/javascript>
- /* Define a human */
- Function Person (name, age ){
- This. name = name; this. age = age;
- }
- /* Define a student class */
- FunctionStudent (name, age, grade ){
- Person. apply (this, arguments); this. grade = grade;
- }
- // Create a student class
- Var student = new Student (qian, 21, first grade );
- // Test
- Alert (name: + student. name + + age: + student. age ++ grade: + student. grade );
- // You can see the test result name: qian age: 21 grade: grade 1.
- // I didn't assign values to the name and age attributes in the student class. Why are there values of these two attributes? This is the magic of apply.
- </Script>
-
- Analysis: Person. apply (this, arguments );
-
- This: student is used to create an object.
-
- Arguments: An array, that is, ["qian", "21", "First Grade"];
-
- In other words, student is used to execute the content in the Person class. this exists in the Person class. in this way, the attributes are created in the student object.
-
-
-
- 2. call example
-
- In the Studen function, you can change the fix in apply to the following:
-
- Person. call (this, name, age );
-
- So OK.
-
- 3. When to use apply and call
-
- When an object parameter is given, if the parameter is in the form of an array, for example, the arguments parameter is passed in the apply example, this parameter is of the array type, in addition, when you call Person, the parameter list is consistent (that is, the first two parameters of Person and Student are consistent). You can use apply, if my Person parameter list is like this (age, name), and Student's parameter list is (name, age, grade), you can use call to implement it, that is, directly specify the location (Person. call (this, age, name, grade ));
-
- 4. Some other clever use of apply
-
- Careful people may have noticed that when I call the apply method, the first parameter is the object (this), and the second parameter is an array set,
-
- When calling Person, he does not need an array, but why does he give me an array? I can still parse the array into parameters one by one,
-
- This is a clever use of apply. You can convert an array into a parameter list by default ([param1, param2, param3] To param1, param2, param3) if we want to use a program to implement every item in the array and replace it with a list of parameters, it may take a while. With this feature of apply, therefore, the following efficient methods are available:
-
-
-
- A) Math. max can be used to obtain the largest entry in the array.
-
- Because the Math. max parameter does not support Math. max ([param1, param2]), that is, the array
-
- However, it supports Math. max (param1, param2, param3 ...), Therefore, we can solve var max = Math. max. apply (null, array) according to the feature of apply, so that we can easily obtain the largest item in an array.
-
- (Apply will replace a number assembly with a parameter followed by a parameter and pass it to the method)
-
- This is the first parameter given a null value during the call. This is because there is no object to call this method. I only need to use this method to help me calculate the result and get the returned result ,. therefore, a null is passed directly.
-
- B) Math. min can be used to obtain the smallest entry in the array.
-
- The same idea as max is var min = Math. min. apply (null, array );
-
- C) Array. prototype. push can merge two arrays.
-
- Similarly, the push method does not provide an array of push, but it provides push (param1, param ,... ParamN) so you can also use apply to replace this array, that is:
-
- Vararr1 = new Array (1, 2, 3 );
- Vararr2 = new Array (4,5, 6 );
- Array. prototype. push. apply (arr1, arr2 );
-
- It can also be understood that arr1 calls the push method, and the parameter is a set of parameters that are assembled into parameter lists by applying.
-
- Under what circumstances can I use apply for special usage such as Math. min:
-
- Generally, the target function only needs n parameter lists, instead of receiving an array ([param1 [, param2 [,... [, ParamN]), you can use the apply method to cleverly solve this problem!
-
-
- 5. Conclusion:
-
- At first, I had no idea about apply. I finally read the application several times and I typed the code several times to understand the intermediate principle. Therefore, no matter what you do, as long as you are willing to move your mind and work on the Code, such a technology will master...
-
- For example, the fourth part cleverly solves the problems that actually exist. This is definitely not a solution that beginners can think of (this is not what I think ), if you do not have a certain understanding of programming, you will not think of this. In a single sentence, accumulating and learning more, improving your abilities and understanding of programming ideas are the most critical!