Data Encapsulation in ES5

This lesson explains the steps to protect properties and teaches how to access these protected properties.

Hiding Data

Till now, all the constructor function properties were easily accessible outside of it, and their values could easily be modified without any restrictions. In JavaScript, both ES5 and ES6 versions, there is no formal support for access modifiers using keywords such as public, private or protected. By default, all the properties are public; hence, can easily be accessed.

Let’s discuss property protection for ES5 version. We will look at the ES6 version later on in the course.

Before we get into the details, let’s take a look at the following term:

internal property: a property or method that can only be accessed by methods present inside the constructor function.

An internal property can then be considered protected from being accessed in an unauthorized manner.

Example

Let’s start by taking a look at how properties were being defined inside the constructor function previously:

Create a free account to access the full course.

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