default `deleteCount` value of `Array.prototype.splice`
Le 03/07/2012 15:09, John-David Dalton a écrit :
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?
What about we say it's a spec bug and say IE8- is wrong while the rest of the planet is right?
@mathias has a suggested fix: mathias.html5.org/specs/javascript/#array.prototype.splice(start,-deletecount-,-item1-,-item2-,-…-))
On Tue, Jul 3, 2012 at 9:09 AM, John-David Dalton < 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> 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 be0
and so return[]
. es5.github.com/#x15.4.4.12"Let actualDeleteCount be min(max(ToIntegeres5.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 docsdeveloper.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.
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 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:
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.
On Jul 3, 2012, at 9:07 AM, Brendan Eich wrote:
Russell Leggett wrote:
...
The spec states: "When the splice method is called with two or more arguments..." - therefore, a.splice(2); leads to unspecified behavior.
Technically that's not correct. The introduction to chapter 15 says:
"Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value."
So, if the second argument is not specified it is treaded as the value undefined and ToInteger(undefined) is 0.
If you look at the MDN docs 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.
No, technically, all the browsers except for IE deviate from the specification and it has been specified that way since the function was introduced in ES3.
Causality was the other way, of course: I implemented splice in SpiderMonkey in 1997 based on Perl 4:
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.
and I have no idea why ES3 did not codify the FF implementation. Prior to the emergence of Safari and Chrome it probably would have been a stretch to say that the FF behavior was a de facto standard and it is always tricky for implementors to navigate through situations where there is divergence between the de jour and de facto standards.
Agree we should codify this in ES6.
Agreed, the ES6 standards and implementations should get aligned on this. In theory it could go either way, but in this case changing the standard to match the majority of implementations seems like the reasonable thing to do.
Does somebody want to file a bug so we can track this?
On Tuesday, July 3, 2012 11:42 AM, Allen Wirfs-Brock wrote:
On Jul 3, 2012, at 9:07 AM, Brendan Eich wrote:
Causality was the other way, of course: I implemented splice in SpiderMonkey in 1997 based on Perl 4:
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.
and I have no idea why ES3 did not codify the FF implementation. Prior to the emergence of Safari and Chrome it probably would have been a stretch to say that the FF behavior was a de facto standard and it is always tricky for implementors to navigate through situations where there is divergence between the de jour and de facto standards.
Looks like this was also discussed in 2010 on this list: esdiscuss/2010-October/012012.
Agree we should codify this in ES6.
Agreed, the ES6 standards and implementations should get aligned on this. In theory it could go either way, but in this case changing the standard to match the majority of implementations seems like the reasonable thing to do.
IE9/10 also implement the single argument extension, so standardizing this in ES6 would align with now de-facto behavior in modern browsers.
Does somebody want to file a bug so we can track this?
Opened bug 429 against the ES6 drafts. ecmascript#429
Luke
Allen Wirfs-Brock wrote:
On Jul 3, 2012, at 9:07 AM, Brendan Eich wrote:
Russell Leggett wrote:
...
The spec states: "When the splice method is called with two or more arguments..." - therefore, a.splice(2); leads to unspecified behavior.
Technically that's not correct.
Note you are replying to Russell here and again later, not to me ;-).
Causality was the other way, of course: I implemented splice in SpiderMonkey in 1997 based on Perl 4:
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.
and I have no idea why ES3 did not codify the FF implementation.
LOL, no Firefox in 1998-1999. Waldemar may recall but it could just be an oversight.
Prior to the emergence of Safari and Chrome it probably would have been a stretch to say that the FF behavior was a de facto standard and it is always tricky for implementors to navigate through situations where there is divergence between the de jour and de facto standards.
Back around '97 when splice went in, Netscape was losing share but still had >60% share.
I wasn't in many ES3 meetings, but I suspect this was just a missed de-facto standard. No Perl fans stood up.
On Tue, Jul 3, 2012 at 6:28 PM, Brendan Eich <brendan at mozilla.org> wrote:
Allen Wirfs-Brock wrote:
On Jul 3, 2012, at 9:07 AM, Brendan Eich wrote:
Russell Leggett wrote:
...
The spec states: "When the splice method is called with two or more arguments..." - therefore, a.splice(2); leads to unspecified behavior.
Technically that's not correct.
Note you are replying to Russell here and again later, not to me ;-).
Haha, yes, sorry, I bow to your superior spec-reading powers. I should not have written with such a confident tone :)
Just checked and IE8 returns
[]
correctly weeee!