Detailed Explanation of Js apply Method

  • 2021-07-21 07:12:18
  • OfStack

I began to see javascript functions apply and call, very vague, look also do not understand, recently saw on the Internet some articles on apply method and call some examples, finally look at a bit of eye-catching, here I do the following notes, I hope to share with you.. If there is anything wrong or unclear place I hope readers will make a lot of comments, in order to improve together.

Mainly, I want to solve the following problems:

1. What is the difference between apply and call

2. When to use apply and when to use call

3. 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

1. 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

2. call example

In the Studen function, you can modify the apply to read as follows:

Person.call(this,name,age); 

This is ok

3. When to use apply and when to use call

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), apply can be used. If my parameter list of 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);

4. One other clever use 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 achieve 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 achieve the smallest 1 item in the array

Similarly, max is an idea 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.

In general, when you can use apply for special uses such as Math. min:

In general, the target function only needs n parameter list, but does not accept the form of an array ([param1 [, param2 [, … [, paramN]]]]), which can be solved skillfully by apply!

5. Summary:

1 I started to apply very do not understand, the last read a few times, knock a few times more code, just understand the middle of the truth, so, no matter what to do, as long as they are willing to use their brains, willing to knock code, such a technology will be mastered …

And for example, in Part 4, Clever to solve the real problems, this is definitely not a beginner can think of the solution (this is not my own thought), no programming has a certain understanding of this, or a sentence, more accumulation, more learning, improve their ability and understanding of programming ideas is the most critical!


Related articles: