An object literal can initialize a JScript Object object. An object literal is represented by a comma-delimited list that is surrounded with a pair of curly braces ({}). Each element of the list is a property followed by a colon and the value of the property. The value can be any valid JScript expression.
Using Object Data
In this example, the variable obj
is initialized to be an object with two properties, x
and y
, with the values 1 and 2 respectively.
В | ![]() |
---|---|
var obj = { x:1, y:2 }; |
Object literals can be nested. In this example, an identifier cylinder
refers to an object with three properties: height
, radius
, and sectionAreas
. The sectionAreas
property is an object with its own properties, top
, bottom
, and side
.
В | ![]() |
---|---|
var r = 3; var h = 2; var cylinder = { height : h, radius : r, sectionAreas : { top : 4*Math.PI*r*r, bottom : 4*Math.PI*r*r, side : 2*Math.PI*r*h } }; |
![]() |
---|
An object literal cannot be used to initialize an instance of a class-based object. The appropriate constructor function must be used to perform the initialization. For more information, see Class-based Objects. |