How to use objects
undefined is an object. Due to their similarity to associative arrays, objects are used to simulate the structs and classes from other languages. It should be noted that objects aren’t really classes since their method of inheritance doesn’t quite work the same way, but they do support inheritance.
Whenever you create an object, through whichever method, the new object will inherit (read: copy) the properties of a so-called prototype object. The new object’s prototype property will hold a reference to that object. Since the properties are merely copied, this means that changing the value of a property in the child object does not modify that value in the parent object. There isn’t such a thing as an instance of a class; creating an object merely results in a collection of copied and independent properties.
Objects created using the simplest approaches (object literals and the new keyword, as discussed below), will automatically use the Object.prototype object as their prototype. This object (which itself has no prototype) includes functions like
hasOwnProperty() which will, through prototypical inheritance, automatically be copied to the new object.
There are three ways to create an object: adding an object literal to your code, using the new keyword, and calling
Object.create(), although the latter is only available in ECMA Script 5.
Creating an object by writing out an object literal is the simplest way of creating an object, and also what happens when you take some JSON code containing objects and evaluate it. Some examples of object literals include:
The object property names may be written without quotes as long as they qualify as variable names. However, since internally they are strings, you can write whatever you want as long as you use quotes:
When referencing a property of an object, you can use either the “dot” notation, as long as the property name qualifies as variable name, or the brackets notation for any string:
When you use an object literal to create an object, its prototype will automatically and inevitably be set to
Object.prototype, thus adding several functions to your new object, including
It should be noted that an object literal creates a new object whenever it is evaluated. When you place an object literal in a loop, a new object will be created each time the loop is executed which may turn out to be expensive.
Object keys are not ordered
Thus, if you do:
then there is no guarantee that
x will contain the names of the object’s fields either in alphabetical order or in the order they were created.
Creating objects with the
A slightly different approach to creating objects is using the
new keyword. To do so, you must provide
The constructor function provided will be stored in the new object’s
prototype property. Note that writing
new Object() is the same as writing an object literal.
Date() are prototype functions that add functions to the new objects, but ultimately also inherit from
Object.prototype and thus the same functions are added to your new object that would have been added in case of an object literal.
Creating objects with Object.create()
ECMA Script 5 adds a new function,
Object.create(). Its first argument allows you to specify the prototype of the new object to be created.
Object.create() is the only way to create objects that have no prototype and thus inherit no functions.
If you do want to inherit from
Object.prototype, then you must write:
which is the same as: