Protecting Properties

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

We'll cover the following

The method to protect the class properties in ES6 is similar to what we discussed in the previous chapter.

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.

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

Example

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

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy