| JavaScript - Objects | | Print | |
Objects
An object is a compound data type that contains any number of properties. Each property has a name and a value. The . operator is used to access a named property of an object. For example, you can read and write property values of an object o as follows:
o.x = 1;
o.y = 2;
o.total = o.x + o.y;
Object properties are not defined in advance as they are in C, C++, or Java; any object can be assigned any property. JavaScript objects are associative arrays: they associate arbitrary data values with arbitrary names. Because of this fact, object properties can also be accessed using array notation:
o["x"] = 1;
o["y"] = 2;
Objects are created with the new operator. You can create a new object with no properties as follows:
var o = new Object( );
Typically, however, you use predefined constructors to create objects that are members of a class of objects and have suitable properties and methods automatically defined. For example, you can create a Date object that represents the current time with:
var now = new Date( );
You can also define your own object classes and corresponding constructors; doing this is documented later in this section.
In JavaScript 1.2 and later, you can use object literal syntax to include objects literally in a program. An object literal is a comma-separated list of name:value pairs, contained within curly braces. For example:
var o = {x:1, y:2, total:3};
See Object (and Date) in the reference section.
| Users' Comments (0) |
|
No comment posted






