Protecting Properties

This lesson explains the steps to hide properties in the ES6 version of classes.

Hiding Properties in ES6

In order to hide the class properties, they can be declared inside the constructor using the var keyword so that they’re only accessible through getters/setters, which can also be defined inside the constructor.

For the ES5 version, the convention of using _ prefix before values that need to be protected is also followed in the ES6 version.


Let’s take a look at an example that implements data hiding in the ES6 version:

Get hands-on with 1200+ tech skills courses.