Getters and setters

When following the steps provided at the end of the previous section, you'll notice that every time you want to update a field, things become troublesome and prone to errors. Let's use a common technique to add a pair of functions, getters, and setters. These are as follows:

  • getters can be used to get values from a frozen object by unfreezing them so that they can be used.
  • setters allow you to modify any property of an object. You can do this by creating a new and updated version of it, leaving the original untouched.

Let's build our getters and setters.

Get Mastering JavaScript Functional Programming - Second Edition now with O’Reilly online learning.

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