Creating a non-enumerable property with Object.defineProperty
We've seen in previous recipes how to avoid having properties overridden. There are situations where we might not want a property to be read. Recall the Object.entries method, which creates an iterator of all the properties and values on the object. Well, that's not precisely true. It creates an iterator of all the enumerable properties.
In this recipe, we'll see how to create properties that won't be included in the iterator.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
06-07-non-enumerable-props. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js.
- Create a
main.jsfile with amainfunction that creates an object with key-value pairs of books and authors:
// main.js export function...