admin管理员组文章数量:1307658
According to MDN, I think, array.splice can take 1 argument:
If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.
but it's not clear whether the one argument option is a SpiderMonkey extension (there's only one syntax example, which is confusing).
It works in Chrome and Firefox, but I don't know of the patibility beyond that. Does anybody know definitively?
According to MDN, I think, array.splice can take 1 argument:
If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.
but it's not clear whether the one argument option is a SpiderMonkey extension (there's only one syntax example, which is confusing).
It works in Chrome and Firefox, but I don't know of the patibility beyond that. Does anybody know definitively?
Share Improve this question asked Apr 22, 2011 at 19:58 SkilldrickSkilldrick 70.9k36 gold badges181 silver badges230 bronze badges3 Answers
Reset to default 7According to 15.4.4.12
of the ECMAScript specification, the only mentioned prototype is:
Array.prototype.splice (start, deleteCount [ , item1 [ , item2 [ , … ] ] ] )
So no, the second parameter is not optional in my reading.
Per spec, at least two arguments are required.
The ability to call with only one argument is a SpiderMonkey extension to the spec. It's entirely possible that Chrome implemented a similar extension. Looks like so did IE9, Opera, and Safari (just tested in those).
Maybe it's time for a spec change....
Yes you can call this function in one of these styles:
Syntax:
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
Parameters:
- start
The index at which to start changing the array.
If greater than the length of the array, start will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many elements as items provided.
If negative, it will begin that many elements from the end of the array. (In this case, the origin -1, meaning -n is the index of the nth last element, and is therefore equivalent to the index of array.length - n.) If start is negative infinity, it will begin from index 0.
- deleteCount [Optional]
An integer indicating the number of elements in the array to remove from start.
If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start), then all the elements from start to the end of the array will be deleted. However, it must not be omitted if there is any item1 parameter.
If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element.
- item1, item2, ... [Optional]
The elements to add to the array, beginning from start.
If you do not specify any elements, splice() will only remove elements from the array.
本文标签: javascriptCan arraysplice() be called with one argumentStack Overflow
版权声明:本文标题:javascript - Can array.splice() be called with one argument? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741827271a2399718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论