Object Literal

Summary

Initialize a JavaScript object using object literal notation, and, optionally, the properties of the object may be specified at creation.

Syntax

{ [ property1: expression1 ] [, property2: expression2 ] [, ... , propertyN: expressionN ] }

Parameters

propertyN
The name of the object property. Can be a valid identifier or string value.
expressionN
Any valid expression.

Description

An object may be created by specifying its properties:

1
2
3
4
5
6
var Saturn = {
    mass: 95.16,
    radius: 9.45,
    name: "Saturn",
    type: "gas giant"
};

which is equivalent to:

1
2
3
4
5
6
7
import Externals.JS;
 
var Saturn = new Object();
Saturn.mass = 95.16;
Saturn.radius = 9.45
Saturn.name = "Saturn";
Saturn.type = "gas giant";

The object literal notation is therefore equivalent to creating a new JavaScript object, creating properties for that object, and assigning values to those properties.

Raw JavaScript objects (such as JavaScript objects initialized with the object literal notation) have the external type.

Examples

Expressions May Be Used to Assign Values to Properties
1
2
3
4
5
6
7
8
int x = 3;
                 
var transformations = {
    identity: x,
    square: x * x,
    multInverse: 1/x,
    addInverse: -x
};
Notation May Be Nested to Create Subproperties
1
2
3
4
5
6
7
8
9
10
11
var meal = {
    appetizer: "nachos",
    salad: {
        greens: "spinach",
        dressing: "ranch"
    },
    entree: "carne asada",
    dessert: "flan"
};
 
string g = meal.salad.greens;

Share

HTML | BBCode | Direct Link