Defining read-only props with Object.defineProperty

It's not always ideal to have methods that can be overridden. By default, properties that are assigned to an object can be reassigned. We need another option to add functions to an object so they won't be changed.

In this recipe, we'll see how to add non-writable properties to an object with Object.defineProperty.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.