close
JavaScript Wiki
Advertisement
A page in the
 
Introduction
 
Basics
Statements ยท Control Flow Statements ยท Comments ยท Objects ยท Functions ยท Style
Features
Scoping ยท Inheritance ยท DOM


It has been suggested that this page or section be merged into Wikibooks:JavaScript. (Discuss)

An object is a great way to keep track of a bunch of related information. Additionally, the information contained in one object can be "inherited" by another. For example, imagine a program which represents cars. Other languages like PHP, Java, or C++ force the programmer to act as a manufacturer, providing a blueprint with every detail of how each vehicle's parts will work before it ever runs. But JavaScript lets you add and remove parts as needed, so you can have a Ferrari one second and a seaplane the next. And if at some point you particularly like the current arrangement, you can instantly make unlimited copies.

Syntax[]

Main article: Object#Literal_syntax

Objects are initialized via braces {}; they may be populated either during initialization or later. The following examples are effectively identical:

var obj = {foo: 'bar'};


var obj = {};
obj.foo = 'bar';

If a property is accessed before being initialized, its value is undefined.

Advertisement