The main thing I want to solve the following problems:
1. What is the difference between apply and call
2. Other clever uses of apply (Usually under what circumstances can apply be used)
I first found the definitions of apply and call from the Internet, and then used examples to explain the meaning of these two methods and how to use them.
apply: The method can hijack another object's method and inherit the properties of another object.
Function.apply(obj,args) method can receive two parameters
obj: This object will replace this object in the Function class
args: This is an array, it will be passed as an argument to Function(args-->arguments)
call: means the same as apply, but the parameter list is different.
Function.call(obj,param1,param2,…,paramN)
obj: This object will replace this object in the Function class
params: This is a parameter list
apply example:
<script type="text/javascript">/*Define 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+"/n"+"grade:"+student.grade);//You can see the test result name:qian age:21 grade: First grade //I did not assign values to name and age attributes in the student class. Why do these two attributes have values? This is the magic of apply.</script>Analysis: Person.apply(this,arguments);
This: When creating an object, it represents student
arguments: is an array, that is ["qian", "21", "first grade"];
In other words, in layman's terms, it is: use student to execute the contents of Person class. There are statements such as this.name in Person class, so that the attributes are created into the student object.
Call Example
In the Studen function, you can modify the apply to the following:
Person.call(this,name,age);
the difference
When giving object parameters, if the parameter form is an array, for example, the parameter arguments is passed in the apply example, this parameter is an array type, and when calling Person, the parameter list is corresponding to the same (that is, the first two digits of Person and Student's parameter list are consistent), you can use apply. If my Person's parameter list is like this (age, name), and Student's parameter list is (name, age, grade), this can be achieved by calling, 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
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 collection.
When calling Person, what he needs is 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 to a parameter list by default ([param1, param2, param3] to param1, param2, param3). If we use the program to implement the implementation of replacing each item in the array with a parameter list, it may take a while. With the help of this feature of apply, we have the following efficient method:
a) Math.max can achieve the largest item in the array
Because Math.max parameter does not support Math.max([param1,param2]) that is, array
However, it supports Math.max(param1,param2,param3...), so you can solve the problem of var max=Math.max.apply(null,array) based on the feature of apply just now, so that you can easily get the largest item in an array
(apply will replace an array assembly with one parameter and pass it to the method after another)
When this part is called, the first parameter is given a null. This is because there is no object to call this method. I just need to use this method to help me calculate and get the returned result. So I directly passed a null over
b) Math.min can achieve the smallest item in the array
Also, it is the same as max
var min=Math.min.apply(null,array);
c) Array.prototype.push can implement the combination of two arrays
The push method does not provide push an array, but it provides push(param1,param,…paramN) so you can also install and replace the array by applying, that is:
vararr1=new Array("1","2","3"); vararr2=new Array("4","5","6"); Array.prototype.push.apply(arr1,arr2);It can also be understood in this way, arr1 calls the push method, and the parameters are replaced by applying the array assembly into a set of parameter lists.
The above is the full understanding of the key input call that the editor brings to you. I hope everyone can support Wulin.com~