Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bug in "splice" using IE6??

0 views
Skip to first unread message

Gary N.

unread,
Jan 26, 2004, 12:41:06 PM1/26/04
to
Hi -

I'm not sure if I've found a bug with the "splice" function or if I just
need better documentation. Splice doesn't work quite how O'Reilly describes
it (Javascript, 4th edition, Jan 2002, section 9.2.6, pg 144). I'm using
IE6.

var a = new Array( 1,2,3,4,5 );
document.write( a.splice( 0 ) ); // returns "", should return "1,2,3,4,5"

It works when you add the (supposedly optional) second argument. For
example:
a.splice( 0, a.length ); // returns "1,2,3,4,5"

Is this a known bug/feature? Is there a good javascript reference site?

Thanks for the help!

Gary

Here is a short block of code:
<SCRIPT language='javascript'>

function testSplice() {
var sample = new Array( 1,2,3,4,5 );
parent.document.write( 'doesn\'t work = ' + sample.splice( 0 ) + '<br>' );
parent.document.write( 'works fine = ' + sample.splice( 0, sample.length )
+ '<br>' );
}

</SCRIPT>

<HTML>
<BODY onload="testSplice();">
text
</BODY>
</HTML>


Evertjan.

unread,
Jan 26, 2004, 1:24:41 PM1/26/04
to
Gary N. wrote on 26 jan 2004 in comp.lang.javascript:

> I'm not sure if I've found a bug with the "splice" function or if I
> just need better documentation. Splice doesn't work quite how
> O'Reilly describes it (Javascript, 4th edition, Jan 2002, section
> 9.2.6, pg 144). I'm using IE6.
>
> var a = new Array( 1,2,3,4,5 );
> document.write( a.splice( 0 ) ); // returns "", should return
> "1,2,3,4,5"
>
> It works when you add the (supposedly optional) second argument. For
> example:
> a.splice( 0, a.length ); // returns "1,2,3,4,5"

DeleteCount, the second parameter is REQUIRED.

At least under jScript.

In practice, you could say it is optional, but defaults to 0 (zero) and
nothing is deleted.

It would be very illogical to have the whole array content deleted.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Lee

unread,
Jan 26, 2004, 1:10:45 PM1/26/04
to
Gary N. said:
>
>Hi -
>
>I'm not sure if I've found a bug with the "splice" function or if I just
>need better documentation. Splice doesn't work quite how O'Reilly describes
>it (Javascript, 4th edition, Jan 2002, section 9.2.6, pg 144). I'm using
>IE6.
>
>var a = new Array( 1,2,3,4,5 );
>document.write( a.splice( 0 ) ); // returns "", should return "1,2,3,4,5"
>
>It works when you add the (supposedly optional) second argument.

You need better documentation.
The second argument is not optional:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsmthsplice.asp>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1193766>

Lasse Reichstein Nielsen

unread,
Jan 26, 2004, 3:28:53 PM1/26/04
to
"Gary N." <gary....@nospam.intel.com> writes:

> I'm not sure if I've found a bug with the "splice" function or if I just
> need better documentation. Splice doesn't work quite how O'Reilly describes
> it (Javascript, 4th edition, Jan 2002, section 9.2.6, pg 144). I'm using
> IE6.

I don't have that book, so I'll go by the ECMAScript standard.

> var a = new Array( 1,2,3,4,5 );
> document.write( a.splice( 0 ) ); // returns "", should return "1,2,3,4,5"

It returns [] (an array with length 0). That is correct behavior. The
second argument is used to find the *length* of the slice to extract,
using
min(max(ToInteger( the-second-argument ),0), length-of-rest-of-array)

If omitted, the second arugment is undefined, and ToInteger (an
utility function defined in the specification that is not in the
language) gives 0 on undefined. So, the deleteCount is 0, which is why
you get a zero-length array back.

Are you confuzing the method "splice" with the one called "slice"?
The second, optional, argument of that one will make it extend
to the end of the array.

> It works when you add the (supposedly optional) second argument. For
> example:
> a.splice( 0, a.length ); // returns "1,2,3,4,5"

It *is* optional. If omitted, it is zero (which makes little sense for
splice, but so it goes).

> Is this a known bug/feature? Is there a good javascript reference site?

The ECMAScript standard (a little dense, but quite precise).
<URL:http://www.mozilla.org/js/language/E262-3.pdf>
Array.prototype.splice is section 15.4.4.2 and and ToInteger is
section 9.4

/L
--
Lasse Reichstein Nielsen - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Lasse Reichstein Nielsen

unread,
Jan 26, 2004, 3:38:59 PM1/26/04
to
Lasse Reichstein Nielsen <l...@hotpop.com> writes:

> It *is* optional.

I take that back. The ECMAScript standard doesn't say what happens if
you call splice with less than two arguments.

If it assumes that the omitted argument is "undefined", it is as I
said. Browsers seem to do that, except Mozilla, which does what you
expected.

Gary N.

unread,
Jan 26, 2004, 7:21:54 PM1/26/04
to
Lasse -

THANK YOU for the JavaScript reference and for looking into splice's
functionality! I'm printing out the manual right now. Other than the
O'Reilly book, I really didn't have a good reference.

Gary


"Lasse Reichstein Nielsen" <l...@hotpop.com> wrote in message
news:d696tp...@hotpop.com...

Guy Roydor

unread,
Jan 27, 2004, 5:56:29 AM1/27/04
to
voir la spécification sur :
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/array.html#1193766

"si deuxième argument est 0 il faut le troisième"
G.Roydor

Lasse Reichstein Nielsen a écrit:

0 new messages