Search⌘ K

Data Encapsulation in ES5

Explore how to implement data encapsulation in ES5 by hiding properties inside constructor functions. Learn to create internal properties with local variables and access them safely through get/set methods. Understand how this approach protects data while allowing controlled interaction with object 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 ...