Grokking Advanced JavaScript
Vote

0% completed

​

Getters and Setters

The object below has a description property whose value is never stored anywhere. Watch what it prints before and after the model changes.

Toyota Corolla Toyota Camry [ 'make', 'model', 'description' ]

The code reads car.description like any other property, with no parentheses, yet the value changes when model changes. The reason is the get keyword. It makes description a property backed by a function. The function runs each time the property is read, and the value that it returns becomes the value of the read. A function of this kind is called a getter.

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content