6Advanced Reference Types
THE OBJECT TYPE
Up to this point, most of the reference-value examples have used the Object
type, which is one of the most commonly used types in ECMAScript. Although instances of Object
don't have much functionality, they are ideally suited to storing and transmitting data around an application.
There are two ways to explicitly create an instance of Object
. The first is to use the new
operator with the Object
constructor like this:
let person = new Object();
person.name = "Matt";
person.age = 29;
The other way is to use object literal notation. Object
literal notation is a shorthand form of object definition designed to simplify creating an object with numerous properties. For example, the following defines the same person object from the previous example using object literal notation:
let person = {
name: "Matt",
age: 29
};
In this example, the left curly brace ({
) signifies the beginning of an object literal because it occurs in an expression context. An expression context in ECMAScript is a context in which a value (expression) is expected. Assignment operators indicate that a value is expected next, ...
Get Professional JavaScript for Web Developers, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.