Data Encapsulation in ES5

This lesson explains the steps to protect properties and teaches us 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 be modified without any restrictions. In both ES5 and ES6 versions of JavaScript, there is no formal support for access modifiers using keywords such as public, private or protected. By default, all properties are public; hence, they can easily be accessed.

Let’s discuss property protection for the ES5 version.

Before we get into 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.


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

Get hands-on with 1200+ tech skills courses.