Grokking Advanced JavaScript
Vote

0% completed

​

Encapsulation

A car's odometer should only count up, yet the object below lets any line of code turn it back to zero. Nothing in the object stops the change, and nothing records that it happened.

Keeping an object's data and the methods that work on that data together in one unit, and allowing outside code to change the data only through those methods, is called encapsulation. The diagram compares two versions of the same car. At the top, outside code writes mileage directly and sets it to 0 and then to -500. At the bottom, mileage is hidden, so outside code can change it only through `drive()

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content