Object literal (Definition)

An object initialiser that creates the object as well.

Availability:

ECMAScript edition - 3
Property/method value type:Object object

JavaScript version 1.2 introduces Object literals.

The object is created and returned by the expression. This would normally be assigned to a variable, which effectively names the object. It isn't the object class but it can be copied. Its class is still "Object".

Object literals can be nested so that the properties of the topmost object can in fact be object literals themselves.

The values assigned to the properties as the object is created can be derived by evaluating a JavaScript expression.

You can add as many properties as you care to but you must be careful to keep the nesting properly balanced.

The result is an object with the properties containing the values described in the literal expression.

Example code:

   // Create a simple object literal
   var simple = { prop:100 };
   // Create a nested object literal
   var nested = { reference: { prop:100 } };
   // Create a nested object literal with expression derived value
   var evaluated = { reference: { prop:(Math.random()*100) } };

See also:Object constant, Object.constructor

Cross-references:

ECMA 262 edition 3 - section - 11.1.5

O'Reilly JavaScript Definitive Guide - page - 45