liveBook · Manning

Chapter 8: Chapter 8. Controlling access to objects

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

The introduction explains why changing JavaScript objects may need monitoring and identifies getters, setters, and proxies as ways to control access.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why changing JavaScript objects may need monitoring and identifies getters, setters, and proxies as ways to control access.

Key terms: dynamic collections of properties, monitor, getters and setters, proxies, ES6

8.1 Controlling access to properties with getters and setters

This section explains why controlling access to object properties is useful and contrasts explicit accessor methods with JavaScript’s built-in getters and setters.

Key terms: private skillLevel variable, getSkillLevel, setSkillLevel, closures, value property, true getters and setters, normal data properties

8.1 Do you know?

This section poses self-check questions about getters, setters, and proxies without explaining the answers.

8.1.1 Defining getters and setters

This section shows how to define getters and setters and how they make ordinary-looking property reads and writes execute methods, including methods that access a closure-held variable.

Key terms: getter, setter, property, object literals, ES6 classes, get, set, Object.defineProperty, property descriptor, closure

8.1.2 Using getters and setters to validate property values

This section shows how a setter can reject invalid property assignments before they change a private value.

Key terms: setter, skillLevel, private _skillLevel variable, integer, exception

8.1.3 Using getters and setters to define computed properties

Explains how getters and setters make a property reflect other properties without storing its own value, and why that can make code clearer.

Key terms: computed properties, getter, setter, fullTitle, name, clan, getFullTitle, internal state

8.2 Using proxies to control access

The section explains how a proxy controls access to a target object through operation-specific traps, demonstrates property reads and writes, and identifies operations that cannot be trapped.

Key terms: proxy, target object, getters and setters, Proxy constructor, traps, get trap, set trap, equality, ==, ===

8.2.1 Using proxies for logging

Shows how a proxy can log object-property reads and writes without putting logging code in each property’s getter and setter.

Key terms: makeLoggable, target object, Proxy, get trap, set trap, getters and setters, domain code, proxy object trap methods

8.2.2 Using proxies for measuring performance

This section shows how a proxy can time function calls without changing the function or its callers.

Key terms: proxy, target object, handler, apply trap, console.time, isPrime

8.2.3 Using proxies to autopopulate properties

This section explains how a proxy can create missing folder properties as they are accessed, allowing a nested path to be built without defining each folder beforehand.

Key terms: proxy, get trap, autopopulating properties, folder object

8.2.4 Using proxies to implement negative array indexes

This section shows how a proxy can give JavaScript arrays negative indexes for reading and writing items from the end.

Key terms: negative array indexes, proxies, proxy, get trap, set trap, unary plus operator, array length

8.2.5 Performance costs of proxies

This section explains why proxies can slow object access, shows how to compare proxied and ordinary array access, and advises where to use proxies cautiously.

Key terms: proxy, traps, layer of indirection, proxied array, standard array, new Date().getTime(), proxies, performance sensitive

8.3 Summary

8.4 Exercises

Concepts explained in this chapter

← Chapter 7: Chapter 7. Object orientation with prototypes Chapter 9: Chapter 9. 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.