JavaScript - Object


// JavaScript - Objects

Different way to create objects:

var obj = new Object();
var obj = {};
var car = { color: 'red', wheels: 4, hubcaps: 'spinning', age: 4};

These are sematically equivalent; the second is called object literal syntax, 
and is more convenient. Object literal syntax was not present in very early 
versions of the language.

Different ways to access an object's property / attribute: = "Simon";
var name =;

obj["name"] = "Simon";
var name = obj["name"];

These are sematically equivalent. The square-brackets method has the advantage 
that the name of the property is provided as a string, which means it can be 
calculated at run-time. It can also be used to set and get properties with 
names that are reserved words:

obj.for = "Simon"; // Syntax error
obj["for"] = "Simon"; // works fine

Objects in JavaScript are mutable keyed collections.  In JavaScript, arrays are 
objects.  Functions are objects.  Regular expressions are objects.  Objects are 

An object is a container of properties, where a property has a name and a value.  
A property name can be any string, including the empty string.  A property 
value can be any JavaScript value except for undefined.

Object literals are a convenient notation for specifying new objects.  The names 
of the properties can be specified as names or as strings.  The names are 
treated as literal names, not as variable names, so the names of the properties 
of the object must be known at compile time.  The values of the properties are 
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License