Chapter 8. Controlling access to objects

 

This chapter covers

  • Using getters and setters to control access to object properties
  • Controlling access to objects through proxies
  • Using proxies for cross-cutting concerns

In the previous chapter, you saw that JavaScript objects are dynamic collections of properties. We can easily add new properties, change the values of properties, and even completely remove existing properties. In many situations (for example, when validating property values, logging, or displaying data in the UI), we need to be able to monitor exactly what’s going on with our objects. So in this chapter, you’ll learn techniques for controlling access to and monitoring all of the changes that occur in your objects.

We’ll start with getters and setters, methods that control access to specific object properties. You first saw these methods in action in chapters 5 and 7. In this chapter, you’ll see some of their built-in language support and how to use them for logging, performing data validation, and defining computed properties.

Do you know?

8.1. Controlling access to properties with getters and setters

8.2. Using proxies to control access

8.3. Summary

8.4. Exercises