Javascript数组复制,concat vs slice,哪一个更好? [英] Javascript array copying, concat vs slice, which one is better?
本文介绍了Javascript数组复制,concat vs slice,哪一个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有两种不同的方法来复制数组,使用Array.concat或Array.slice,
there are two different ways to copy a array, using Array.concat or Array.slice,
例如:
var a = [1, 2, 3],
c1 = [].concat(a),
c2 = a.slice(0);
哪种方式更好?
推荐答案
为清楚起见,您应该使用记录方法获取数组副本(或其中一部分)的方法:
For clarity, you should use the method that is the documented method of taking a copy of an array (or portion thereof):
var c2 = a.slice(0);
在任何不错的浏览器上,性能差异都可以忽略不计,因此清晰度应该是决定因素。
On any decent browser the performance difference will be negligible, so clarity should be the deciding factor.
这篇关于Javascript数组复制,concat vs slice,哪一个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文