JS knowledge Comb--apply () use detailed

Source: Internet
Author: User

Reprint: Original address: http://blog.csdn.net/business122/article/details/8000676

Js Apply method Details 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,  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. Mainly I want to solve a few problems:1What is the difference between apply and call?2. Under what circumstances use apply, under what circumstances call3Other clever uses of apply (generally under what circumstances you can use apply) 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. Apply: Methods can hijack another object's method, inheriting another object's properties. Function.apply (Obj,args) method can receive two parameters obj: This object will replace the function class in the This object args: This is an array, which is passed as a parameter to function (args-arguments) Call: Same as apply, except that the parameter list is not the same. Function.call (obj,[param1[,param2[,... [, Paramn]]]) OBJ: This object will replace the function class in the This object params: This is a list of parameters1. 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 classvarStudent=NewStudent ("Qian", 21, "first Grade")); //TestAlert ("Name:" +student.name+ "\ n" + "Age:" +student.age+ "\ n" + "Grade:" +Student.grade); //You can see the test results Name:qian age:21 Grade: First grade//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.</script>Analysis: person.apply ( This, arguments); This: In the creation of the object at this time represents the Studentarguments: is an array, that is, ["Qian", "21st" ," first grade "]; that is, to use student to execute the contents of the person class, such as this.name in the person class, so that the attribute is created into the student object.2the. Call example can be modified in the Studen function to read as follows: Person.call ( This, name,age); so it's OK.3. Under what circumstances to use apply, under what circumstances to use the call in the case of parameters to the object, if the parameter is in the form of an array, such as the apply example passed the parameter arguments, this parameter is the array type, And when the person is called, the list of arguments is consistent (that is, the first two bits of the argument list for person and student) can be applied, if my person's argument list is (Age,name), The Student parameter list is (Name,age,grade), so it can be implemented with call, that is, the position of the corresponding value of the parameter list is directly specified (Person.call ( This, Age,name,grade));4Some other ingenious usages of apply may have noticed that when I invoke the Apply method, the first argument is the object ( This), the second argument is an array collection, and 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, which is an ingenious use of apply, You can convert an array by default to a list of arguments ([param1,param2,param3] to PARAM1,PARAM2,PARAM3) This might take a while if we use a program that implements each item of the array as a list of parameters. With this feature of apply, we have the following high-efficiency methods: A) Math.max can be implemented to get the largest of the array because the Math.max parameter does not support Math.max ([PARAM1,PARAM2]) It is an array but it supports Math.max (param1,param2,param3 ...), so it can be solved according to the characteristics of the just apply .varMax=math.max.apply (NULL, array), so that it is easy to get the largest item in an array (apply will change a number assembly to a parameter to a parameter to pass to the method) this block is called when the first parameter gives a null, this is because there is no object to call this method, I just need to use this method to help me to get the result of the return. So directly passed a null past B) Math.min can be implemented to get the smallest of an array of the same and Max is an ideavarMin=math.min.apply (NULL, array); c) Array.prototype.push can implement two array merges the same 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, namely: Vararr1=NewArray ("1", "2", "3"); VARARR2=NewArray ("4", "5", "6"); Array.prototype.push.apply (ARR1,ARR2); 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. Usually under what circumstances, apply a special usage like math.min and so on: generally in the target function only need n parameter list, Instead of receiving an array form ([param1[,param2[,... [, Paramn]]] ), you can solve this problem skillfully by using the "apply" method!

JS knowledge Comb--apply () use 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.