Basically I'm going to solve a few questions:
1. What is the difference between apply and call?
2. Other ingenious uses of apply (in general, under what circumstances can apply)
I first looked at the definition of apply and call from the Internet, and then I used an example to explain the meaning of the two methods and how to use them.
Apply: Method 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 This object in the function class
Args: This is an array, which is passed as a parameter to the function (args-->arguments)
Call: The same as apply, except that the argument list is not the same.
Function.call (obj,param1,param2,..., Paramn)
OBJ: This object will replace the This object in the function class
Params: This is a parameter list
Apply Example:
<script type= "Text/javascript" >
/* Defines 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+" \ n "+" Age: "+student.age+" \ "+" Grade: "+student.grade);
You can see the test results Name:qian age:21 Grade: First grade
//Student Class I did not assign values to the name and age attributes, why are the values of these two attributes, this is the magic
of the apply. </script>
Analysis: Person.apply (this,arguments);
This: At this time the object is created to represent the student
Arguments: is an array, namely ["Qian", "21", "first grade"];
That is, the popular point is: with student to carry out the content of the class of person, in the class of person there are this.name, such as such statements, so that the attributes are created into the student object
Call Example
In the Studen function, you can modify the apply to read as follows:
Person.call (This,name,age);
Difference
In the case of an object parameter, if the argument is in the form of an array, such as passing the parameter arguments in the apply example, the parameter is an array type and the list of arguments is consistent when the person is called ( That is, the first two digits of the list of person and student are consistent) and apply, if the argument list for my person is such (Age,name), and the Student argument list is (Name,age,grade), This can be done using call, which is directly specifying the position of the corresponding value of the parameter list (Person.call (This,age,name,grade));
Some other ingenious uses of apply
A careful person may have noticed that the first parameter is the object (this) when I call the Apply method, and the second argument is an array collection.
When calling person, he doesn't need an array, but why did he give me an array I can still parse an array into one parameter,
This is an ingenious use of apply, which converts an array by default to a list of arguments ([param1,param2,param3] converted to PARAM1,PARAM2,PARAM3) this if you let us use the program to implement each item of the array, To fit the list of parameters, it may take a while, with the use of this feature, so there are the following efficient methods:
A) Math.max can achieve the largest of the array
Because the Math.max parameter does not support Math.max ([PARAM1,PARAM2]) is an array
But it supports Math.max (param1,param2,param3 ...), so you can solve the Var max=math.max.apply (Null,array) According to the feature that you just apply. It's easy to get one of the largest in an array
(Apply will assemble a number into a parameter to pass to the method)
This is the first parameter given to a null at the time of the call, because there is no object to call this method, I just need to use this method to help me compute, get the result of return on the line. So directly passed a null past
b) Math.min can achieve the smallest of the array
Same and Max is an idea
var min=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 a push (Param1,param,... paramn) So you can also use the apply to replace the array, namely:
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 invokes the push method, where the parameter is assembled by apply to the collection of parameter lists.
The above is a small series for you to bring a comprehensive understanding of the constructor inheritance key apply call all content, I hope that we support the cloud Habitat Community ~