default `deleteCount` value of `Array.prototype.splice`

Brendan Eich brendan at mozilla.org
Tue Jul 3 09:07:33 PDT 2012


Russell Leggett wrote:
> On Tue, Jul 3, 2012 at 9:09 AM, John-David Dalton 
> <john.david.dalton at gmail.com <mailto:john.david.dalton at gmail.com>> wrote:
>
>     Just checked and IE8 returns `[]` correctly weeee!
>
>     On Tue, Jul 3, 2012 at 8:49 AM, John-David Dalton
>     <john.david.dalton at gmail.com <mailto:john.david.dalton at gmail.com>>
>     wrote:
>
>         I recently saw something like
>
>         var a = ['a', 'b', 'c', 'd'];
>         a.splice(2); // -> ['c', 'd'] in latest Chrome, Opera,
>         Firefox, Safari, Rhino, RingoJS, you name it
>
>         by spec though I thought the `deleteCount` should be `0` and
>         so return `[]`.
>         http://es5.github.com/#x15.4.4.12
>
>         "Let /actualDeleteCount/ be min(max(ToInteger
>         <http://es5.github.com/#x9.4>(/deleteCount/),0),/len/ –/ actualStart/)."
>
>         var a = ['a', 'b', 'c', 'd'];
>         a.splice(2, 0); // -> []
>
>         Am I reading it wrong or is this a bug in most engines?
>
>
>
> The spec states: "When the splice method is called with two or more 
> arguments..." - therefore, a.splice(2); leads to unspecified behavior. 
> If you look at the MDN docs 
> <https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice>, 
> splice called with only a single argument removes until the end of the 
> array, similar to the substring method.
>
>     "If no howMany parameter is specified (second syntax above, which 
> is a SpiderMonkey extension), all elements after index are removed."
>
> Basically, what is happening here is that everyone else accepted the 
> spidermonkey extension, but technically because it is unspecified, 
> browsers can do what they want and still be considered compliant. 
> Perhaps it is something which should be *added* to the spec.

Causality was the other way, of course: I implemented splice in 
SpiderMonkey in 1997 based on Perl 4:

http://perldoc.perl.org/functions/splice.html

and indeed it handles omitted OFFSET and LENGTH the same. IE8 is just 
defying a de-facto standard, news at 11 :-P.

Agree we should codify this in ES6.

/be


More information about the es-discuss mailing list