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 = "Nicholas";
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: "Nicholas",
age: 29
};
In this example, the left curly brace ({
) signifies the beginning of an object literal because it occurs in an expression...