JavaScript apply
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
The apply()
method calls a function with a given this
value, and arguments
provided as an array (or an array-like object).
Note: While the syntax of this function is almost identical to that of call()
, the fundamental difference is that call()
accepts an argument list, while apply()
accepts a single array of arguments.
Syntax Section
function
.apply(thisArg, [argsArray])
Parameters Section
thisArg
- The value of
this
provided for the call tofunc
. Note thatthis
may not be the actual value seen by the method: if the method is a function in non-strict mode code,null
andundefined
will be replaced with the global object, and primitive values will be boxed. This argument is not optional argsArray
- Optional. An array-like object, specifying the arguments with which
func
should be called, ornull
orundefined
if no arguments should be provided to the function. Starting with ECMAScript 5 these arguments can be a generic array-like object instead of an array. See below for browser compatibility information.
Return value Section
The result of calling the function with the specified this
value and arguments.
Description Section
You can assign a different this
object when calling an existing function. this
refers to the current object, the calling object. With apply
, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.
apply
is very similar to call()
, except for the type of arguments it supports. You use an arguments array instead of a list of arguments (parameters). With apply
, you can also use an array literal, for example, func.apply(this, ['eat', 'bananas'])
, or an Array
object, for example, func.apply(this, new Array('eat', 'bananas'))
.
You can also use arguments
for the argsArray
parameter. arguments
is a local variable of a function. It can be used for all unspecified arguments of the called object. Thus, you do not have to know the arguments of the called object when you use the apply
method. You can use arguments
to pass all the arguments to the called object. The called object is then responsible for handling the arguments.
Since ECMAScript 5th Edition you can also use any kind of object which is array-like, so in practice, this means it's going to have a property length
and integer properties in the range (0..length-1)
. As an example you can now use a NodeList
or a custom object like { 'length': 2, '0': 'eat', '1': 'bananas' }
.
Most browsers, including Chrome 14 and Internet Explorer 9, still do not accept array-like objects and will throw an exception.
Examples Section
Using apply
to append an array to another Section
We can use push
to append an element to an array. And, because push
accepts a variable number of arguments, we can also push multiple elements at once. But, if we pass an array to push
, it will actually add that array as a single element, instead of adding the elements individually, so we end up with an array inside an array. What if that is not what we want? concat
does have the behaviour we want in this case, but it does not actually append to the existing array but creates and returns a new array. But we wanted to append to our existing array... So what now? Write a loop? Surely not?
apply
to the rescue!
1 2 3 4 | var array = [ 'a' , 'b' ]; var elements = [0, 1, 2]; array.push.apply(array, elements); console.info(array); // ["a", "b", 0, 1, 2] |
https://www.w3schools.com/js/js_function_apply.asp
Method Reuse
With the apply()
method, you can write a method that can be used on different objects.
The JavaScript apply() Method
The apply()
method is similar to the call()
method (previous chapter).
In this example the fullName method of person is applied on person1:
var person = { fullName: function() { return this.firstName + " " + this.lastName; } } var person1 = { firstName: "Mary", lastName: "Doe" } person.fullName.apply(person1); // Will return "Mary Doe"
The Difference Between call() and apply()
The difference is:
The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
The apply() method is very handy if you want to use an array instead of an argument list.
The apply() Method with Arguments
The apply()
method accepts arguments in an array:
var person = { fullName: function(city, country) { return this.firstName + " " + this.lastName + "," + city + "," + country; } } var person1 = { firstName:"John", lastName: "Doe" } person.fullName.apply(person1, ["Oslo", "Norway"]); //这里是一个array参数
Compared with the call()
method:
var person = { fullName: function(city, country) { return this.firstName + " " + this.lastName + "," + city + "," + country; } } var person1 = { firstName:"John", lastName: "Doe" } person.fullName.call(person1, "Oslo", "Norway"); //这里是2个参数
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-06-30 Git的日常处理流程
2016-06-30 modelstate.isvalid false
2016-06-30 Razor Intro
2015-06-30 makeBackronym
2015-06-30 Regular Ball Super Ball
2014-06-30 winform中splitter的用法
2014-06-30 VS中 Winform查看窗体内控件之间的相互关系