tags:

views:

95

answers:

2

I am writing Tetris in JavaScript as practice, and i am somewhat confused by the array notation that i am using. Here's the array that i have that stores all my pieces.

var pieces = [[[1,1],
               [1,1]],

              [[1,0],
               [1,0],
               [1,1]],

              [[0,1],
               [0,1],
               [1,1]],


              [[0,1,0],
               [1,1,1]],

              [[1,0],
               [1,1],
               [0,1]],

              [[0,1],
               [1,1],
               [1,0]]];

What is this notation called? How is this difference from just saying "new array()"?

Thank you!

+4  A: 

This is an array literal, you create the array and supply the values in one go.

var a = []; is the same as var a = new Array();

But new Array() will give you an empty array, there is no way to supply values simultaneously the way you can with the literal syntax.

j-g-faustus
Actually you can: `new Array(1,2,3)` gives you the same as `[1,2,3]`. However there is a trap there in that `new Array(10)` is an array of ten undefineds, not an array containing one integer! IMO: stick to the array literal syntax, it is also easier to read. It didn't work in Netscape 3, but y'know I think we can live with that in this century.
bobince
A: 

It's called JSON, Javascript Object Notation.

Leventix