bug in "splice" using IE6??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary N.

    bug in "splice" using IE6??

    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='javas cript'>

    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="testSpl ice();">
    text
    </BODY>
    </HTML>


  • Evertjan.

    #2
    Re: bug in &quot;splice&qu ot; using IE6??

    Gary N. wrote on 26 jan 2004 in comp.lang.javas cript:
    [color=blue]
    > 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"[/color]

    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)

    Comment

    • Lee

      #3
      Re: bug in &quot;splice&qu ot; using IE6??

      Gary N. said:[color=blue]
      >
      >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.[/color]

      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.netscap e.com/library/manuals/2000/javascript/1.5/reference/array.html#1193 766>

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: bug in &quot;splice&qu ot; using IE6??

        "Gary N." <gary.newell@no spam.intel.com> writes:
        [color=blue]
        > 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.[/color]

        I don't have that book, so I'll go by the ECMAScript standard.
        [color=blue]
        > var a = new Array( 1,2,3,4,5 );
        > document.write( a.splice( 0 ) ); // returns "", should return "1,2,3,4,5"[/color]

        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(ToInteg er( 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.
        [color=blue]
        > It works when you add the (supposedly optional) second argument. For
        > example:
        > a.splice( 0, a.length ); // returns "1,2,3,4,5"[/color]

        It *is* optional. If omitted, it is zero (which makes little sense for
        splice, but so it goes).
        [color=blue]
        > Is this a known bug/feature? Is there a good javascript reference site?[/color]

        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 - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: bug in &quot;splice&qu ot; using IE6??

          Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
          [color=blue]
          > It *is* optional.[/color]

          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
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Gary N.

            #6
            Re: bug in &quot;splice&qu ot; using IE6??

            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" <lrn@hotpop.com > wrote in message
            news:d696tpgc.f sf@hotpop.com.. .[color=blue]
            > Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
            >[color=green]
            > > It *is* optional.[/color]
            >
            > 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
            > --
            > Lasse Reichstein Nielsen - lrn@hotpop.com
            > DHTML Death Colors:[/color]
            <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
            > 'Faith without judgement merely degrades the spirit divine.'[/color]


            Comment

            • Guy Roydor

              #7
              Re: bug in &quot;splice&qu ot; using IE6??

              voir la spécification sur :


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

              Lasse Reichstein Nielsen a écrit:[color=blue]
              > Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
              >
              >[color=green]
              >>It *is* optional.[/color]
              >
              >
              > 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[/color]

              Comment

              Working...