有没有比这更好的方法在javascript中将一个数组拼接到另一个数组中
var string = 'theArray.splice('+start+', '+number+',"'+newItemsArray.join('","')+'");';
eval(string);
您可以使用apply https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply避免评估:
var args = [start, number].concat(newItemsArray);
Array.prototype.splice.apply(theArray, args);
The apply https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply函数用于调用另一个函数,并使用给定的上下文和参数(以数组形式提供),例如:
如果我们打电话:
var nums = [1,2,3,4];
Math.min.apply(Math, nums);
apply 函数将执行:
Math.min(1,2,3,4);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)