Home >Web Front-end >JS Tutorial >Detailed explanation of the use of Js apply() (including code)

Detailed explanation of the use of Js apply() (including code)

亚连
亚连Original
2018-05-18 10:05:151328browse

The following is the use of Js apply() that I compiled for everyone. Interested students can take a look.

Js apply method detailed explanation

When I first saw the javascript functions apply and call, they were very blurry and I couldn’t understand them. I recently read them online After reading some examples of the apply method and call in some articles, I finally got a glimpse of them. Here I will make the following notes and hope to share them with you. If there is anything wrong or the explanation is not clear, I hope readers can give me some comments. , in order to improve together..

Mainly I want to solve a few problems:

1. What is the difference between apply and call

2. Under what circumstances should apply be used? , when to use call

3. Other clever uses of apply (generally under what circumstances can apply be used)

I first found the definitions of apply and call from the Internet, and then used Examples are provided to explain the meaning of these two methods and how to use them.

Apply: The method can hijack the method of another object 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, which will be passed to Function as a parameter (args--> arguments)

call: The meaning is 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

1.apply示例:  
<script type="text/javascript">   
/*定义一个人类*/   
function Person(name,age) {   
    this.name=name; this.age=age;   
}   
 /*定义一个学生类*/   
functionStudent(name,age,grade) {   
    Person.apply(this,arguments); this.grade=grade;   
}   
//创建一个学生类   
var student=new Student("qian",21,"一年级");   
//测试   
alert("name:"+student.name+"\n"+"age:"+student.age+"\n"+"grade:"+student.grade);   
//大家可以看到测试结果name:qian age:21 grade:一年级   
//学生类里面我没有给name和age属性赋值啊,为什么又存在这两个属性的值呢,这个就是apply的神奇之处.   
</script>

Analysis: Person.apply(this,arguments);

this: When creating an object, it represents the student at this time

arguments:是一个数组,也就是[“qian”,”21”,”一年级”];

In other words, in layman’s terms: use student to execute the content in the Person class, and this exists in the Person class .name, etc., thus creating the attributes into the student object

2.call example

In the Student function, you can modify the apply to the following:

Person.call(this,name,age);

This is ok

3. When to use apply and when to use call

When giving object parameters, if the parameter is in the form of an array, such as apply The parameter arguments is passed in the example. This parameter is of array type, and the list of parameters is consistent when calling Person (that is, the first two digits of the parameter lists of Person and Student 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), so it can be implemented with call, that is, directly specifying the position of the corresponding value in the parameter list (Person.call (this,age,name,grade));

4.Some other clever uses of apply

Careful people may have noticed that when I call the apply method, the first The parameter is an object (this), and the second parameter is an array collection.

When calling Person, what he needs is not an array, but why did he give me an array? I can still parse the array into an array. A parameter,

This is a clever use of apply. It can convert an array into a parameter list by default ([param1, param2, param3] is converted into param1, param2, param3). If we let It may take a while to use a program to convert each item of the array into a list of parameters. 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 the Math.max parameter does not support Math.max([param1, param2]), which is an array

But it does Math.max(param1, param2, param3...), so you can solve it based on the characteristics of apply just now var max=Math.max.apply(null, array), so you can easily get the largest item in an array

(apply will convert an array into a parameter and pass it to the method one by one)

         这块在调用的时候第一个参数给了一个null,这个是因为没有对象去调用这个方法,我只需要用这个方法帮我运算,得到返回的结果就行,.所以直接传递了一个null过去  

b)Math.min  可以实现得到数组中最小的一项  

同样和 max是一个思想 var min=Math.min.apply(null,array);  

c)Array.prototype.push 可以实现两个数组合并  

同样push方法没有提供push一个数组,但是它提供了push(param1,param,…paramN) 所以同样也可以通过apply来装换一下这个数组,即:  

vararr1=new Array("1","2","3");   
 vararr2=new Array("4","5","6");   
Array.prototype.push.apply(arr1,arr2);

也可以这样理解,arr1调用了push方法,参数是通过apply将数组装换为参数列表的集合.  

通常在什么情况下,可以使用apply类似Math.min等之类的特殊用法:  

一般在目标函数只需要n个参数列表,而不接收一个数组的形式([param1[,param2[,…[,paramN]]]]),可以通过apply的方式巧妙地解决这个问题!  

5.总结:  

一开始我对apply 非常的不懂,最后多看了几遍,自己多敲了几遍代码,才明白了中间的道理,所以,不管做什么事情,只要自己肯动脑子,肯动手敲代码,这样一个技术就会掌握…     

还有比如第四部分得内容,巧妙的解决了实实在在存在的问题,这个肯定不是一个初学者能想到的解决方案(这个也不是我自己想的),没有对编程有一定认识的不会想到这个的,还是一句话,多积累,多学习,提升自己的能力和对编程思想的理解能力才是最关键!  

上面是我整理给大家的Js apply()使用,希望今后会对大家有帮助。

相关文章:

简单易懂,javascript自学学习笔记

在页面自动执行(加载)js的几种解决方法,详细代码解析

利用js 来判断客户端能否上网(代码附上)

The above is the detailed content of Detailed explanation of the use of Js apply() (including code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn