jQuery的merge()方法的作用是修改第一个数组,使得它末尾加上第二个数组。其实,允许第二个参数不为数组。
$.merge([0,1,2], [2,3,4]) == [0, 1, 2, 2, 3, 4]
jQuery的merge()源码如下:
merge: function( first, second ) {
var l = second.length, i = first.length, j = 0;
// second可以是一个对象
if (typeof l === "number" ) { // 如果second是数组
for (; j < l; j++)
first[i++] = second[j];
}
else { // 如果second不是数组
// 如second = {}; second[0] = 1; second[1] = 2; second[2] = 3;
while (second[j] !== undefined)
first[i++] = second[j++];
}
first.length = i;
return first;
},