JavaScript – Object and Array Initializers

Object and array initializers are
expressions whose value is a newly created object or array. These
initializer expressions are sometimes called “object literals” and
“array literals.” Unlike true literals, however, they are not primary
expressions, because they include a number of subexpressions that
specify property and element values. Array initializers have a
slightly simpler syntax, and we’ll begin with those.

An array initializer is a comma-separated list of expressions
contained within square brackets. The value of an array initializer is
a newly created array. The elements of this new array are initialized
to the values of the comma-separated expressions:

[]         // An empty array: no expressions inside brackets means no elements
[1+2,3+4]  // A 2-element array.  First element is 3, second is 7

The element expressions in an array initializer can themselves
be array initializers, which means that these expressions can create
nested arrays:

var matrix = [[1,2,3], [4,5,6], [7,8,9]];

The element expressions in an array initializer are evaluated
each time the array initializer is evaluated. This means that the
value of an array initializer expression may be different each time it
is evaluated.

Undefined elements can be included in an array literal by simply
omitting a value between commas. For example, the following array
contains five elements, including three undefined elements:

var sparseArray = [1,,,,5];

A single trailing comma is allowed after the last expression in
an array initializer and does not create an undefined
element.

Object initializer expressions are like array initializer
expressions, but the square brackets are replaced by curly brackets,
and each subexpression is prefixed with a property name and a
colon:

var p = { x:2.3, y:-1.2 };  // An object with 2 properties
var q = {};                 // An empty object with no properties
q.x = 2.3; q.y = -1.2;      // Now q has the same properties as p

Object literals can be nested. For example:

var rectangle = { upperLeft:  { x: 2, y: 2 },
                  lowerRight: { x: 4, y: 5 } };

The expressions in an object initializer are evaluated each time
the object initializer is evaluated, and they need not have constant
values: they can be arbitrary JavaScript expressions. Also, the
property names in object literals may be strings rather than
identifiers (this is useful to specify property names that are
reserved words or are otherwise not legal identifiers):

var side = 1;
var square = { "upperLeft":  { x: p.x, y: p.y },
               'lowerRight': { x: p.x + side, y: p.y + side}};

We’ll see object and array initializers again in Chapters 6 and 7.

Comments are closed.