Js apply () and call () Methods

Source: Internet
Author: User

Js apply () and call () Methods

 

  1. Js apply method details
  2. 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 ..
  3.  
  4. I want to solve the following problems:
  5.  
  6. 1. What is the difference between apply and call?
  7.  
  8. 2. When to use apply and call
  9.  
  10. 3. Other clever use of apply (usually when can I use apply)
  11.  
  12. 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.
  13.  
  14. Apply: the method that can hijack another object and inherit the attributes of another object.
  15.  
  16. The Function. apply (obj, args) method can receive two parameters.
  17. Obj: this object will replace the Function classThisObject
  18. Args: This is an array and it will be passed as a parameter to Function (args --> arguments)
  19.  
  20. Call: it means the same as apply, except that the parameter list is different.
  21.  
  22. Function. call (obj, [param1 [, param2 [,... [, ParamN])
  23. Obj: this object will replace the Function classThisObject
  24. Params: this is a list of parameters.
  25.  
  26. 1. apply example:
  27.  
  28. <Script type = text/javascript>
  29. /* Define a human */
  30. FunctionPerson (name, age ){
  31. This. Name = name;This. Age = age;
  32. }
  33. /* Define a student class */
  34. FunctionStudent (name, age, grade ){
  35. Person. apply (This, Arguments );This. Grade = grade;
  36. }
  37. // Create a student class
  38. VarStudent =NewStudent (qian, 21, first grade );
  39. // Test
  40. Alert (name: + student. name + + age: + student. age ++ grade: + student. grade );
  41. // You can see the test result name: qian age: 21 grade: grade 1.
  42. // 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.
  43. </Script>
  44.  
  45. Analysis: Person. apply (This, Arguments );
  46.  
  47. This: Student is used to create an object.
  48.  
  49. Arguments: An array, that is, ["qian", "21", "First Grade"];
  50.  
  51. 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.
  52.  
  53.  
  54.  
  55. 2. call example
  56.  
  57. In the Studen function, you can change the fix in apply to the following:
  58.  
  59. Person. call (This, Name, age );
  60.  
  61. So OK.
  62.  
  63. 3. When to use apply and call
  64.  
  65. 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 ));
  66.  
  67. 4. Some other clever use of apply
  68.  
  69. 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,
  70.  
  71. 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,
  72.  
  73. 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:
  74.  
  75.  
  76.  
  77. A) Math. max can be used to obtain the largest entry in the array.
  78.  
  79. Because the Math. max parameter does not support Math. max ([param1, param2]), that is, the array
  80.  
  81. 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.
  82.  
  83. (Apply will replace a number assembly with a parameter followed by a parameter and pass it to the method)
  84.  
  85. 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
  86.  
  87. B) Math. min can be used to obtain the smallest entry in the array.
  88.  
  89. Same as maxVarMin = Math. min. apply (Null, Array );
  90.  
  91. C) Array. prototype. push can merge two arrays.
  92.  
  93. 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:
  94.  
  95. Vararr1 =NewArray (1, 2, 3 );
  96. Vararr2 =NewArray (4,5, 6 );
  97. Array. prototype. push. apply (arr1, arr2 );
  98.  
  99. 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.
  100.  
  101. Under what circumstances can I use apply for special usage such as Math. min:
  102.  
  103. 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!
  104.  
  105.  
  106. 5. Conclusion:
  107.  
  108. 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...
  109.  
  110. 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
    1. Js apply method details
    2. 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 ..
    3.  
    4. I want to solve the following problems:
    5.  
    6. 1. What is the difference between apply and call?
    7.  
    8. 2. When to use apply and call
    9.  
    10. 3. Other clever use of apply (usually when can I use apply)
    11.  
    12. 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.
    13.  
    14. Apply: the method that can hijack another object and inherit the attributes of another object.
    15.  
    16. The Function. apply (obj, args) method can receive two parameters.
    17. Obj: this object will replace this object in the Function class.
    18. Args: This is an array and it will be passed as a parameter to Function (args --> arguments)
    19.  
    20. Call: it means the same as apply, except that the parameter list is different.
    21.  
    22. Function. call (obj, [param1 [, param2 [,... [, ParamN])
    23. Obj: this object will replace this object in the Function class.
    24. Params: this is a list of parameters.
    25.  
    26. 1. apply example:
    27.  
    28. <Script type = text/javascript>
    29. /* Define a human */
    30. Function Person (name, age ){
    31. This. name = name; this. age = age;
    32. }
    33. /* Define a student class */
    34. FunctionStudent (name, age, grade ){
    35. Person. apply (this, arguments); this. grade = grade;
    36. }
    37. // Create a student class
    38. Var student = new Student (qian, 21, first grade );
    39. // Test
    40. Alert (name: + student. name + + age: + student. age ++ grade: + student. grade );
    41. // You can see the test result name: qian age: 21 grade: grade 1.
    42. // 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.
    43. </Script>
    44.  
    45. Analysis: Person. apply (this, arguments );
    46.  
    47. This: student is used to create an object.
    48.  
    49. Arguments: An array, that is, ["qian", "21", "First Grade"];
    50.  
    51. 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.
    52.  
    53.  
    54.  
    55. 2. call example
    56.  
    57. In the Studen function, you can change the fix in apply to the following:
    58.  
    59. Person. call (this, name, age );
    60.  
    61. So OK.
    62.  
    63. 3. When to use apply and call
    64.  
    65. 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 ));
    66.  
    67. 4. Some other clever use of apply
    68.  
    69. 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,
    70.  
    71. 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,
    72.  
    73. 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:
    74.  
    75.  
    76.  
    77. A) Math. max can be used to obtain the largest entry in the array.
    78.  
    79. Because the Math. max parameter does not support Math. max ([param1, param2]), that is, the array
    80.  
    81. 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.
    82.  
    83. (Apply will replace a number assembly with a parameter followed by a parameter and pass it to the method)
    84.  
    85. 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.
    86.  
    87. B) Math. min can be used to obtain the smallest entry in the array.
    88.  
    89. The same idea as max is var min = Math. min. apply (null, array );
    90.  
    91. C) Array. prototype. push can merge two arrays.
    92.  
    93. 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:
    94.  
    95. Vararr1 = new Array (1, 2, 3 );
    96. Vararr2 = new Array (4,5, 6 );
    97. Array. prototype. push. apply (arr1, arr2 );
    98.  
    99. 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.
    100.  
    101. Under what circumstances can I use apply for special usage such as Math. min:
    102.  
    103. 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!
    104.  
    105.  
    106. 5. Conclusion:
    107.  
    108. 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...
    109.  
    110. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.