6Collection Reference Types


  • Working with objects
  • Working with arrays and typed arrays
  • Working with Map, WeakMap, Set, and WeakSet types


Please note that all the code examples for this chapter are available as a part of this chapter's code download on the book's website at www.wrox.com/go/projavascript4e on the Download Code tab.


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 context. An expression context in ECMAScript is a context in which a value (expression) is expected. Assignment operators indicate ...

Get Professional JavaScript for Web Developers, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.