JavaScript built-in objects
First, I looked up the apply()
call()
definition of and from the Internet. Then use examples to explain the meaning and usage of the two methods.
- apply ():Method can hijack another object's method and inherit the properties of another object.
Function.apply (Obj,args): method capable of receiving two parameters
OBJ: This object will replace the This object in the function class
Args: This is an array, which is passed as a parameter to function (args-->arguments)
code example:
<script type= "Text/javascript" > /* Define a human * * function person (name,age) { this.name=name; this.age=age; } /* Define a Student class */ function Student (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+ "\ n" + "Age:" +student.age+ "\ n" + "Grade:" +student.grade); You can see the test results Name:qian age:21 grade: First grade //Student Class I did not assign a value to the name and the age attribute, ah, why there are the values 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 student
Arguments: is an array, i.e. ["Qian", "21", "first grade"];
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
Call (): method can hijack another object's method, inheriting another object's properties. (As with apply, it's just a different argument list.)
Function.call (obj,[param1[,param2[,... [, Paramn]]])
OBJ: This object will replace the This object in the function class
Params: This is a list of parameters
The code example (in the student function can be modified in apply to the following):
Person.call (This,name,age);
The result is the same as the final display
Under what circumstances to use apply, under what circumstances use call?
In the case of a given object parameter
- If the argument is in the form of an array, such as a 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 for people and student are consistent) You can use apply
- If my person's argument list is (Age,name), and Student's argument list is (Name,age,grade), it can be implemented with call, that is, directly specifying the position of the corresponding value of the parameter list (Person.call (this , Age,name,grade));
Some other clever uses of apply
When invoking the Apply () method, the first argument is the object (this), the second argument is an array collection, and when the person is called, he needs not an array, but why does he give me an array I can still parse the array into one parameter, 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 the use of this feature, so there are the following efficient methods:
-
-
Math.max can be implemented to get the largest item in the array
-
Because the Math.max parameter does not support Math.max ([PARAM1,PARAM2]), which is an array but it supports Math.max (Param1,param2,param3 ...), it can be solved according to the characteristics of the just apply
-
here, at the time of the invocation of the first argument to NULL, because there is no object to call this method, just use this method to do the operation, get the result returned is the line, so directly passed a null past
-
var max=Math.max.apply(null,array)
-
-
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 it is also possible to replace this array with apply, namely:
-
var arr1=new Array ("1", "2", "3"), var arr2=new array ("4", "5", "6"); Array.prototype.push.apply (ARR1,ARR2);//arr1 called the Push method, and the argument is a collection of the number assembly as a parameter list by apply
The question about the apply and call method, is to look at someone else's code, so on a variety of check, a lot of bloggers have written very good, summed up the knowledge and popular language let me this chicken very easy to understand ~ for the big hearts. A reference to this article: "JS in the use of the method of application of the small discussion"
Javascript apply () and call () methods