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 badges
Add a ment  | 

3 Answers 3

Reset to default 7

According 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:

  1. 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.


  1. 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.


  1. 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