Bas van der Veer wrote:
Hi,
if I put the line:
var T = new Array(-1);
anywhere in my script, it stops working. e.g
<SCRIPT LANGUAGE="JAVASCRIPT">
<script type="text/javascript">
<!--
Remove. Not needed.
var T = new Array(-1);
Generates the following error in IE: Array.length must be a positive
finite integer.
Firefox errors with: invalid array length
-->
Remove. Not needed.
Is that a bug in both their javascript interpreters or can anybody
give me a good explanation of why that is?
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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq