English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
我在一开始看到JavaScript的函数apply和call时,非常模糊,看也看不懂。最近在网上看到一些文章对apply方法和call的一些示例,总算是看得有点眉目了。在这里我做如下笔记,希望和大家分享。如有不对的或者说法不明确的地方,希望读者多多提一些意见,以便共同提高。
主要我是要解决以下几个问题:
1.apply和call的区别在哪里
2When to use apply and when to use call
3.apply的其他巧妙用法(一般在什么情况下可以使用apply)
I first found the definition of apply and call on the Internet, and then used examples to explain the meaning of these two methods and how to use them.
apply: This method can hijack another object's method and inherit another object's properties.
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, which will be passed as parameters to Function (args-->arguments)
call: The meaning of call is the same as apply, but the parameter list is different.
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"> /*Define a human class*/ 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 year"); //Test alert("name:"+student.name+"\n"+"age:"+student.age+"\n"+"grade:"+student.grade); //As you can see from the test results, name: qian age:21 grade: first year //I didn't assign the name and age properties in the student class, why do these two properties exist? This is the magic of apply. </script>
Analysis: Person.apply(this, arguments);
this: At this time, it represents the student when creating an object
arguments: It is an array, that is, ["qian", "21”, "first year"];
To put it simply, it means using 'student' to execute the content of the Person class. In the Person class, there are statements like this.name, and in this way, the properties are created in the student object
2.call example
In the Studen function, you can modify apply as follows:
Person.call(this, name, age);
This is fine
3When to use apply and when to use call
When providing object parameters, if the form of the parameters is an array, for example, the parameters passed in the apply example are of array type, and the parameter list when calling Person is consistent (that is, the first two parameters of Person and Student are consistent), we can use apply. If my Person parameter list is (age, name), and the Student parameter list is (name, age, grade), we can use call to implement it, that is, directly specify the position of the corresponding parameter list (Person.call(this, age, name, grade));
4.Some other clever uses of .apply
People who are careful 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, it does not need an array. But why can I still parse the array into individual parameters even though I give me an array?
This is a clever use of apply, which can convert an array into a parameter list ([param1,param2,param3] to convert it to param1,param2,param3) This may take some effort to implement if we want to use a program to convert each item of the array into a parameter list. With the help of this characteristic of apply, we have the following efficient method:
a) Math.max can be used to get the largest element in an array
Because Math.max does not support Math.max([param1,param2]) which is an array
However, it supports Math.max(param1,param2,param3...), so we can solve it according to the characteristics of apply just now, var max = Math.max.apply(null, array), which can easily get the largest element in an array
(apply will convert an array into a parameter followed by a parameter to pass to the method)
The first parameter is given null when calling this, because there is no object to invoke this method. I just need to use this method to help me calculate and get the return result. Therefore, null is passed directly
b) Math.min can be used to get the smallest element in an array
Similarly, var min = Math.min.apply(null, array); is a concept
c) Array.prototype.push can merge two arrays
Similarly, the push method does not provide push an array, but it provides push(param1,param,…paramN) So it can also be transformed into this array through apply, 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 like this, arr1Called the push method, the parameters are a collection of parameter lists converted from the array by apply.
Under what circumstances can you use apply similar to Math.min and other special usages:
Generally, the target function only needs n parameter lists and does not accept an array form ([param1[,param2[,...,[paramN]]]]), can be solved cleverly through the apply method!
5.Summary:
At first, I was very confused about apply, after reading it several times and writing several lines of code myself, I finally understood the logic inside. So, no matter what you do, as long as you are willing to think and write code, you will master this technology...
For example, the fourth part of the content, ingeniously solves practical problems, this is definitely not a solution that a beginner can think of (this is not what I thought), those who do not have a certain understanding of programming will not think of this. In short, accumulate more, learn more, and improve your ability and understanding of programming thoughts is the most important!
That's all for the content of this article. I hope the content of this article can bring you some help in learning or working, and I also hope to support the Yelling Tutorial more!
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report infringement, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)