Today I've heard that it's possible to create a multi - dimensional array in js using this syntax:
var a = new Array(3,3);
a[2][2] = 2;
alert(a[2][2])
However this doesn't work in opera. Am I wrong somewhere?
Today I've heard that it's possible to create a multi - dimensional array in js using this syntax:
However this doesn't work in opera. Am I wrong somewhere? |
||||
Yes, you are wrong somewhere. The array constructor is one of the worst parts of the JavaScript language design. Given a single value, it determines the length of the array. Given multiple values, it uses them to initialise the array. Always use the There is no short-cut syntax for creating an array of arrays. You have to construct each one separately.
|
|||||||||||||
|
The code you posted makes an array consisting of two integers. You're then trying to treat an integer as an array.
There is no way to directly instantiate a multidimensional array. |
|||
|
you want to create an array of arrays, but you're creating an array with 2 elements:
if you want to create a multidimensional array, you have to think in terms of array of arrays.
|
|||
|
new Array()
. If you pass only one argument, you indeed set the length of the array to that argument. But if you pass more than one, it generates an array containing those elements. There is no need to useArray()
, use literal notation instead[]
. – Felix Kling Feb 10 '11 at 20:28