JavaScript的如何Array.prototype.push会连接 [英] javascript How Array.prototype.push concatenates
本文介绍了JavaScript的如何Array.prototype.push会连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我见过使用Array的push方法来代替串联,但我不能完全肯定它是如何工作的。
I've seen the Array's push method used to replace concatenation, but I'm not entirely sure how it works.
var a = [1,2,3];
var b = [4,5,6];
Array.prototype.push.apply(a,b);
这是如何串连的地方,而不是返回一个新的数组?
How does this concatenate in place rather than returning a new array?
推荐答案
<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply\"><$c$c>.apply()$c$c>有两个参数:
.apply()
takes two arguments:
fun.apply(thisArg[, argsArray])
您正在传递 A
作为你的这个
对象和 B
为您的参数列表,让你的code是真的叫 .push()
与的元素中,b
为你的论点:
You're passing a
as your this
object and b
as your argument list, so your code is really calling .push()
with the elements of b
as your arguments:
var a = [1, 2, 3];
a.push(4, 5, 6);
现在, .push()
只是变异原来的数组。
Now, .push()
just mutates your original array.
这篇关于JavaScript的如何Array.prototype.push会连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文