Property descriptors

Once we have found a satisfactory solution to control the visibility of an object's members, we have to face how public members can be accessed and which constraints we can set.

Controlling access to public properties

When we define public properties, we do not put any constraints on their accessibility. Consider the literal person definition:

var person = { name: "John", surname: "Smith"}; 

Public properties are readable and writable and they can be set to any value. The following assignments are perfectly legal:

var personName = person.name; 
 
person.name = "Mario"; 
person.surname = [1, 2, 3]; 

But for the meaning we want to give to the object, these assignments might not make sense or not be desirable. Assigning an array to a person's ...

Get Mastering JavaScript Object-Oriented Programming now with O’Reilly online learning.

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