What's wrong with: var T = new Array(-1);

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bas van der Veer

    What's wrong with: var T = new Array(-1);

    Hi,

    if I put the line:

    var T = new Array(-1);

    anywhere in my script, it stops working. e.g

    <SCRIPT LANGUAGE="JAVAS CRIPT">
    <!--
    var T = new Array(-1);
    document.open() ;
    document.write( 'hahahah');
    -->
    </SCRIPT>

    doesn't produce any output. If I change

    var T = new Array(-1);

    into

    var T = new Array(1);

    or

    var T = new Array(-1,-1);

    everything works fine again.
    Same results in Mozilla 1.7.2 as in IE 6.

    Is that a bug in both their javascript interpreters or can anybody
    give me a good explanation of why that is?


    Bas



  • Andre Herbst

    #2
    Re: What's wrong with: var T = new Array(-1);

    Bas van der Veer wrote:[color=blue]
    > Is that a bug in both their javascript interpreters or can anybody
    > give me a good explanation of why that is?[/color]

    Yes, its very simple:

    If you put more than one arguments in the brakets then it will be
    interpreted as values which will be put into the array. But if you only use
    one argument it will be interpreted as the dimension for the array. If you
    put a
    -1 there the interpreter tries to declare an array with -1 fields. That
    causes the error. You could write instead:

    var T = new Array()
    T[0] = -1

    or

    var T = new Array("-1") // but here the -1 is saved as a string not as a
    number; so it must be convertzed to a number again if you which to calculate
    with it


    Comment

    • Mick White

      #3
      Re: What's wrong with: var T = new Array(-1);

      Andre Herbst wrote:
      [color=blue]
      >
      > Yes, its very simple:
      >
      > If you put more than one arguments in the brakets then it will be
      > interpreted as values which will be put into the array. But if you only use
      > one argument it will be interpreted as the dimension for the array. If you
      > put a
      > -1 there the interpreter tries to declare an array with -1 fields. That
      > causes the error. You could write instead:
      >
      > var T = new Array()
      > T[0] = -1
      >
      > or
      >
      > var T = new Array("-1")[/color]

      or var T=[-1]
      Mick

      // but here the -1 is saved as a string not as a[color=blue]
      > number; so it must be convertzed to a number again if you which to calculate
      > with it
      >
      >[/color]

      Comment

      • Grant Wagner

        #4
        Re: What's wrong with: var T = new Array(-1);

        Bas van der Veer wrote:
        [color=blue]
        > Hi,
        >
        > if I put the line:
        >
        > var T = new Array(-1);
        >
        > anywhere in my script, it stops working. e.g
        >
        > <SCRIPT LANGUAGE="JAVAS CRIPT">[/color]

        <script type="text/javascript">
        [color=blue]
        > <!--[/color]

        Remove. Not needed.
        [color=blue]
        > var T = new Array(-1);[/color]

        Generates the following error in IE: Array.length must be a positive
        finite integer.

        Firefox errors with: invalid array length

        [color=blue]
        > -->[/color]

        Remove. Not needed.
        [color=blue]
        > Is that a bug in both their javascript interpreters or can anybody
        > give me a good explanation of why that is?[/color]

        It is obvious to us that you one a one element array containing the
        value -1, but it's not quite so obvious to the JavaScript
        implementations in IE and Firefox. They see a constructor being passed
        a single integer (albeit negative) and try to construct an Array with
        length one. This makes sense, since the underlying implementation
        probably has multiple constructor signatures, one of which probably
        resembles:

        public Array(int len)

        that constructor will get called when new Array(int) is called,
        regardless of whether int is positive or negative. Of course, they
        could have added code to that constructor to test for negative ints
        and called the constructor that takes a list of Array elements, but
        presumably they had their reasons not to.

        To resolve the problem you have a couple of options:

        var a = new Array();
        a[0] = -1; // a.push(-1);

        or

        a = [ -1 ];

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: What's wrong with: var T = new Array(-1);

          Grant Wagner wrote:
          [color=blue]
          > Bas van der Veer wrote:[color=green]
          >> <!--[/color]
          >
          > Remove. Not needed.
          >
          > [...][color=green]
          >> -->[/color]
          >
          > Remove. Not needed.[/color]

          More, the latter is syntactically wrong. `--' is the decrement operator
          that has no valid (variable) operand here. That is why the obsolete
          practice of commenting out the content of "script" elements required
          this masked as a ECMAScript/J(ava)Script comment -- `//-->' or similar.


          PointedEars

          Comment

          Working...