Js Apply Call Method detailed

Source: Internet
Author: User

  1. Js Apply Method Detailed
  2. I saw in the beginning of the JavaScript function apply and call, very vague, see also do not understand, recently saw some articles on the Web on the Apply method and some examples of call, finally is a bit of a look, here I do the following notes, I hope to share with you. If there is something wrong or unclear, I would like to ask readers to make a lot of suggestions in order to improve together.
  3. Mainly I want to solve a few problems:
  4. What is the difference between 1.apply and call?
  5. 2. Under what circumstances use apply, and under what circumstances call
  6. Other clever uses of 3.apply (generally under what circumstances you can use apply)
  7. I first looked at the definition of apply and call from the Internet, and then used examples to explain the meaning of the two methods and how to use them.
  8. Apply: Methods can hijack another object's method, inheriting another object's properties.
  9. Function.apply (Obj,args) method can receive two parameters
  10. OBJ: This object will replace the this object in the function class
  11. Args: This is an array, which is passed as a parameter to function (args-->arguments)
  12. Call: Same as apply, except that the argument list is not the same.
  13. Function.call (obj,[param1[,param2[,... [, Paramn]]])
  14. OBJ: This object will replace the this object in the function class
  15. Params: This is a list of parameters
  16. 1.apply Example:
  17. <script type="Text/javascript" >
  18. /* Define a human */
  19. function Person (name,age) {
  20. This.name=name;   This.age=age;
  21. }
  22. / * Define a Student class * /
  23. Functionstudent (Name,age,grade) {
  24. Person.apply (this,arguments);   This.grade=grade;
  25. }
  26. Create a Student class
  27. var student=New Student ("Qian", "first grade");
  28. Test
  29. Alert ("name:" +student.name+"\ n" +"Age:" +student.age+"\ n" +"Grade:" +student.grade);
  30. You can see the test results Name:qian age:21 Grade: First Grade
  31. I did not assign a value to the name and age attribute in the student class, why is there the value of these two properties, this is the magic of apply.
  32. </script>
  33. Analysis: person.apply (this,arguments);
  34. This: In the creation of the object at this time represents the student
  35. Arguments: is an array, i.e. ["Qian", "21", "first grade"];
  36. That is, the popular point is: Use student to execute the contents of the person class, in the person class inside the existence of this.name and other statements, so that the property is created into the student object
  37. 2.call Example
  38. In the Studen function, you can modify the apply to read as follows:
  39. Person.call (this,name,age);
  40. That's OK.
  41. 3. Under what circumstances use apply, and under what circumstances call
  42. In the case of an object parameter, if the argument is in the form of an array, such as the parameter arguments is passed in the apply example, the parameter is an array type, and the list of parameters is consistent when the person is called ( That is, the first two bits of the parameter list of the person and student are consistent) can be applied, if my person's parameter list is such (Age,name), and Student's argument list is (Name,age,grade),  This can be done with call, that is, directly specify the position of the corresponding value of the parameter list (Person.call (this,age,name,grade));
  43. Some other clever uses of 4.apply
  44. The attentive person may have noticed that when I invoke the Apply method, the first argument is the object (this), the second argument is an array collection,
  45. When the person is called, he doesn't need an array, but why does he give me an array I can still parse the array into one parameter,
  46. This is a clever use of apply, you can convert an array by default to a parameter list ([PARAM1,PARAM2,PARAM3] to PARAM1,PARAM2,PARAM3) This if let us use the program to implement the array of each item, To load a list of parameters, it may take a while, with this feature of apply, the following efficient methods are available:
  47. A) Math.max can be implemented to get the largest item in the array
  48. Because the Math.max parameter does not support Math.max ([PARAM1,PARAM2]), which is the array
  49. But it supports Math.max (param1,param2,param3 ...), so you can solve the var max=math.max.apply (Null,array) According to the characteristics of the just apply. It's so easy to get one of the largest items in an array
  50. (Apply will change a number assembly to a parameter by passing it to a method)
  51. This block in the call when the first parameter gave a null, this is because there is no object to call this method, I just need to use this method to help me to calculate the return of the results on the line. So it passed directly a null past
  52. b) Math.min can be implemented to get the smallest item in the array
  53. Likewise and Max is a thought var min=math.min.apply (Null,array);
  54. c) Array.prototype.push can implement two array merging
  55. The same push method does not provide an array of push, but it provides push (Param1,param,... paramn) So it is also possible to replace this array with apply, namely:
  56. vararr1=New Array ("1","2","3");
  57. vararr2=New Array ("4","5","6");
  58. Array.prototype.push.apply (ARR1,ARR2);
  59. It is also possible to understand that arr1 invokes the push method, which is a set of parameters that can be assembled into a parameter list by using apply.
  60. Under what circumstances, you can use a special usage like math.min and so on:
  61. In general, the target function requires only n argument lists, not the form of an array ([param1[,param2[,... [, Paramn]]] ), you can solve this problem skillfully by apply way!
  62. 5. Summary:
  63. At first I do not understand the apply very much, the last to see a few times, I have to knock a few times the code, only to understand the middle of the truth, so, no matter what to do, as long as they are willing to move the brain, willing to strike code, such a technology will be mastered ...
  64. There are, for example, the fourth part of the content, ingenious solution to the real problem, this is certainly not a beginner can think of the solution (this is not my own thinking), do not have a certain understanding of programming will not think of this, or a sentence, more accumulation, more study, The most important thing is to improve your ability and understanding of programming ideas!

Js Apply Call Method detailed

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.