| re: passing an array element to a function
"csx" <csx@ms.com> wrote in message
news:blfjii$ibk$1@hercules.btinternet.com...[color=blue]
> Hi all,
> Here's a simple problem I'm having problems with. How do I pass an array
> element to a function.
>
> For instance,
>
> values[0] = new values(1, 1,'A',0);
> values[1] = new values(2, 2,'B',1);
> values[2] = new values(3, 3,'C',2);[/color]
Running your code gave me this fatal error: 'values' is undefined.
The bit after the word "new" is expected to be an object.
"Array" is a built-in object, "values" is not.
You are looking for this syntax:
values=new Array(); // should precede the first values[n]
values[0]=new Array( 1, 1, 'A', 0 );
values[1]=new Array( 2, 2, 'B', 1, new Array( 4, '4,5', 5 ) );
values[2]=[ values[1][4] ,'hello', world ];
alert( values ); // Array 1,1,A,0,2,2,B,1,4,4,5,5
alert( values[0] ); // Array 1,1,A,0
alert( values[0][2] ); // A
alert( values[1][2] ); // B
alert( values[1][4][1] ); // 4,5
alert( values[2][0]); // Array 4,4,5,5 // !
alert( values[2][2]); // error: 'world' is undefined
The square brackets used to initiate values[2] are a short-hand way of
saying "new Array". They 're not as well supported across browsers but quite
logical if you get used to them.
HTH
Ivo |