JavaScript的"移"与"拼接" - 这些语句平等的吗? [英] Javascript "shift" versus "splice" - are these statements equal?
问题描述
我只是想确认如果以下两个JavaScript语句产生相同的结果,因为它似乎对我说:
I just want to confirm if the following two Javascript statements produces the same results, as it seems to me:
首先:
var element = my_array.splice(0,1)[0];
二:
var element = my_array.shift();
我想替换由第二首,在我自己的code,以提高可读性。我可以这样做吗?
I want to substitute the first by the second, in my own code, to improve readability. Can I do this?
推荐答案
他们将有同样的效果,是的。 拼接(0,1)
将删除 my_array
的第一个元素,并返回一个包含该元素的新数组。 移
将尽相同,但返回元素本身,而不是一个数组。
They will have the same effect, yes. splice(0, 1)
will remove the first element from my_array
and return a new array containing that element. shift
will do the same, but return the element itself, not an array.
移
更可读的(在我看来),也是显著快(在Chrome至少):
shift
is more readable (in my opinion) and is also significantly faster (in Chrome at least):
这篇关于JavaScript的"移"与"拼接" - 这些语句平等的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!