bug in "splice" using IE6??

G

Gary N.

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>
 
L

Lee

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>
 
E

Evertjan.

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

Lasse Reichstein Nielsen

Gary N. said:
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
 
L

Lasse Reichstein Nielsen

Lasse Reichstein Nielsen said:
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.

/L
 
G

Gary N.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top