Jef*_*rey 4 javascript arrays concatenation
我已经看到Array的push方法用于替换连接,但我不完全确定它是如何工作的.
var a = [1,2,3];
var b = [4,5,6];
Array.prototype.push.apply(a,b);
Run Code Online (Sandbox Code Playgroud)
这是如何连接而不是返回一个新数组?
Ble*_*der 15
.apply() 有两个参数:
fun.apply(thisArg[, argsArray])
Run Code Online (Sandbox Code Playgroud)
您将a作为this对象和b参数列表传递,因此您的代码实际上是.push()使用b作为参数的元素调用:
var a = [1, 2, 3];
a.push(4, 5, 6);
Run Code Online (Sandbox Code Playgroud)
现在,.push()只是改变原始数组.