Comprehensive understanding of constructor inheritance key apply call

  • 2021-07-06 09:46:09
  • OfStack

Mainly, I want to solve the following problems:

1. What is the difference between apply and call

2. Other clever uses of apply (1. Under what circumstances can apply be used)

I first looked up 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: A method can hijack the method of another object and inherit the properties of another object.

The Function. apply (obj, args) method can accept two parameters

obj: This object will replace the this object in the Function class

args: This is an array that will be passed as a parameter to Function (args-- > arguments)

call: The meaning of apply is the same as that of apply, except that the parameter 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">
/* Definition 1 Individual human */
function Person(name,age) {
  this.name=name; this.age=age;
}
 /* Definition 1 Student class */
functionStudent(name,age,grade) {
  Person.apply(this,arguments); this.grade=grade;
}
// Create 1 Student class 
var student=new Student("qian",21,"1 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:1 Grade 
// I didn't give it to the student class name And age Attribute assignment , Why are there values for these two attributes , This is it apply The magic of .
</script>

Analysis: Person. apply (this, arguments);

this: student is represented at this time when the object is created

arguments: Is an array, which is ["qian", "21", "Grade 1"];

That is to say, the popular point is to use student to execute the contents of the class Person, and there are statements such as this. name in the class Person, so the attribute is 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 giving object parameters, If the parameter is in the form of an array, For example, the apply example passes the parameter arguments, This parameter is of array type, and when calling Person, the parameter list corresponds to 1 (that is, the first two bits of the parameter list of Person and Student are 1), so apply can be used. If the parameter list of my Person is like this (age, name) and the parameter list of Student is (name, age, grade), it can be implemented with call, that is, directly specify 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 argument is an object (this), the second argument is an array collection,

When calling Person, he didn't need an array, but why did he give me an array and I can still parse the array into one argument,

This is an ingenious use of apply, which can convert an array into a parameter list by default ([param1, param2, param3] into param1, param2, param3). If we use the program to realize every item of the array, to install and change it into a parameter list, it may take 1 time. With the help of this characteristic of apply, there are the following efficient methods:

a) Math. max can get the largest 1 item in the array

Because Math. max ([param1, param2]) is not supported in the Math. max parameter, that is, arrays

But it supports Math. max (param1, param2, param3 …), so we can solve var max=Math. max. apply (null, array) according to the characteristics of apply just now, so we can easily get the largest item in an array

(apply will replace 1 number assembly with 1 parameter followed by 1 parameter passed to the method)

When this piece is called, the first parameter gives an null, this is because there is no object to call this method, I only need to use this method to help me calculate and get the returned result, so I directly passed an null in the past

b) Math. min can get the smallest item in the array

It is also an idea with max


var min=Math.min.apply(null,array);

c) Array. prototype. push enables two combinations of numbers and

Similarly, the push method does not provide push1 arrays, but it does provide push (param1, param, … paramN), so it is also possible to replace this array with apply, 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 calls the push method, and the parameters are the collection of numbers assembled into parameter lists through apply.


Related articles: