liveBook · Manning

Chapter 9: Objects, prototypes, and classes

What you will learn in chapter 9 of Secrets of the JavaScript Ninja, Third Edition

The introduction explains why JavaScript objects are more than key-value pairs and why prototypes still matter in an era of classes.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why JavaScript objects are more than key-value pairs and why prototypes still matter in an era of classes.

Key terms: object properties, Proxy, prototype, prototypes, classes, inheritance, TypeScript

9.1 Customizing objects with properties

The section introduces getter and setter methods as a way to customize what happens when an object property is read or written.

Key terms: properties, skillLevel, getter and setter methods

9.1.1 Defining getters and setters

This section explains how JavaScript getters and setters make property access run methods, why that is useful, and how to add them to existing objects.

Key terms: getter, setter, get, set, firstNinja, direct property access, validation, logging, internal representation, Object.defineProperty

9.1.2 Defining and redefining object properties

This section explains how Object.defineProperty descriptors control whether properties are enumerated, accessed, reassigned, or redefined.

Key terms: Object.defineProperty, descriptor, enumerable, non-enumerable, symbol, Object.keys, Object.assign, spread syntax, accessor descriptor, get

9.2 Intercepting object access with Proxy and Reflect

This section explains how proxies intercept operations on objects, how Reflect can pass operations through, and where proxies are useful or costly.

Key terms: Proxy, target, handler, traps, get, set, Reflect, Reflect.get, Reflect.set, receiver

9.3 Dynamic inheritance with prototypes

Introduces inheritance as a way to reuse and organize code, and identifies prototypes as the foundation of JavaScript classes.

Key terms: inheritance, prototype objects, constructor functions, classes

9.3.1 Fallbacks as inheritance

This section explains how prototype chains provide fallback property access, how to set up the example chain, and how to distinguish an object's own properties from inherited ones.

Key terms: prototype, prototype chain, prototype chaining, fallback, Object.setPrototypeOf, data properties, methods, inheritance, in operator, own property

9.3.2 Building prototyped objects with constructors

This section explains how constructors create objects linked to prototypes, how those prototypes can change inherited behavior, and why prototype-free objects and classes offer different trade-offs.

Key terms: constructor, new keyword, prototype, X.prototype, arrow functions, object literal, Object.prototype, Object.create(null), prototype-free objects, key-value store

9.4 Classic OOP with class and extends

Introduces classes as JavaScript’s way to write classic object-oriented code while retaining its existing function and prototype rules.

Key terms: multi-paradigmatic, classes, ES2015, object-oriented programming (OOP), functions, prototypes

9.4.1 Classes as shorthand for prototypes

This subsection explains how class syntax defines constructors and prototype properties, and how to distinguish and inspect class instances.

Key terms: class, constructor, new, prototype, instances, static, instanceof, typeof, prototype chain, constructor property

9.4.2 Inheriting across classes

This section explains how classes inherit through prototype chaining, how super refers to a parent class, and what limits the section places on inheritance.

Key terms: extends, parent-child relationship, prototype chaining, super, parent class, constructor, this, static properties, linear inheritance, multiple inheritance

9.4.3 Restricting access with private elements

This section explains how class private fields restrict access, how they differ from ordinary properties, and why they are useful.

Key terms: private element syntax, private fields, class definition, dot syntax, proxied, enumerated, deleted, internal value, getter/setter pair, developer ergonomics

9.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: Functions for the future: promises and generators Chapter 10: Dealing with collections →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.