liveBook · Manning

Chapter 7: Chapter 7. Object orientation with prototypes

What you will learn in chapter 7 of Secrets of the JavaScript Ninja, Second Edition

Introduces prototypes as the basis for JavaScript’s approach to object-oriented code.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces prototypes as the basis for JavaScript’s approach to object-oriented code.

Key terms: prototype, property, object-oriented

7.1 Understanding prototypes

This section explains how JavaScript objects use prototypes to find properties on other objects and form a prototype chain.

Key terms: object, property, object-literal notation, inheritance, prototyping, [[prototype]], Object.setPrototypeOf, prototype chain, in operator

7.1 Do you know?

This section poses three self-check questions about object property access, prototype chains, and ES6 classes without explaining their answers.

7.2 Object construction and prototypes

This section explains how calling a function with new creates an object that can use methods on the function’s prototype.

Key terms: constructor function, new operator, constructor, this, function context, prototype, Ninja.prototype, swingSword, instance properties

7.2.1 Instance properties

This section explains how constructors create instance properties, how those properties take precedence over prototype properties, and why shared methods usually belong on the prototype.

Key terms: constructor, new operator, this, instance properties, instance members, prototype, prototype chain, swingSword, instance, private object variables

7.2.2 Side effects of the dynamic nature of JavaScript

This section shows how changing a prototype at runtime affects existing and newly constructed objects differently.

Key terms: prototype, swingSword method, function’s prototype, prototype chain, object instantiation, pierce method

7.2.3 Object typing via constructors

This section shows how an instance’s constructor property identifies its originating function, can help check its origin, and can be used to create another instance.

Key terms: constructor property, constructor function, prototype, instance, typeof operator, instanceof operator, constructor

7.3 Achieving inheritance

The section explains how JavaScript achieves inheritance through a prototype chain rather than by copying properties.

Key terms: inheritance, prototype, copying, prototype chain, Ninja.prototype, Person instance, dance method, instanceof, Person.prototype, inherited function prototypes

7.3.1 The problem of overriding the constructor property

Explains why replacing Ninja’s prototype makes instances report the wrong constructor, and how property descriptors let the code restore that connection without exposing it during enumeration.

Key terms: Ninja.prototype, constructor property, prototype, property descriptor, configurable, enumerable, value, writable, get, Object.defineProperty

7.3.2 The instanceof operator

Explains how JavaScript’s instanceof operator checks a prototype chain and why changing a constructor’s prototype can change the result.

Key terms: instanceof operator, prototype chain, Ninja, Person, instanceof caveat, constructor prototype

7.4 Using JavaScript “classes” in ES6

Explains why ES6 introduced class syntax and clarifies that JavaScript classes still rely on prototype inheritance.

Key terms: class keyword, prototype inheritance, classical inheritance, class-based inheritance

7.4.1 Using the class keyword

This section shows how ES6 classes create objects and methods, explains their relationship to prototypes, and distinguishes static methods from instance methods.

Key terms: class keyword, constructor, this, new, instance, method, syntactic sugar, prototypes, ES6 classes, ES5

7.4.2 Implementing inheritance

The section shows how ES6 classes simplify inheritance, using Person and Ninja to illustrate extends, super, and inherited behavior.

Key terms: extends, Person, Ninja, inheritance, super, constructor, derived, base, prototype, constructor property

7.5 Summary

7.6 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 6: Chapter 6. Functions for the future: generators and promises Chapter 8: Chapter 8. Controlling access to objects →

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.