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

B

Bas van der Veer

Hi,

if I put the line:

var T = new Array(-1);

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

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
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
 
A

Andre Herbst

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

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
 
M

Mick White

Andre said:
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")

or var T=[-1]
Mick

// but here the -1 is saved as a string not as a
 
G

Grant Wagner

Bas said:
Hi,

if I put the line:

var T = new Array(-1);

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

<SCRIPT LANGUAGE="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 ];
 
T

Thomas 'PointedEars' Lahn

Grant said:
Bas said:

Remove. Not needed.

[...]

Remove. Not needed.

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top