Search⌘ K

Data Protection in OOPs

Explore how to protect data in JavaScript object-oriented programming by hiding internal properties within constructor functions and classes. Understand how encapsulation limits external access and controls modification through public methods, enhancing security and code integrity.

Background

Now that we know about constructor functions, classes, and how they achieve OOPs, let’s focus on security. At times, it is important for properties to be hidden or stay inside of an object. We can achieve this data encapsulation through two approaches. Let’s check them out.

Hiding properties

Up until now, all constructor function and class properties were easily accessible outside of it and their values could be modified without any restrictions. In JavaScript ES5 and ES6, there is no formal support for access modifiers using keywords such as public, private, or protected which are keywords used by other languages. By default, all properties are public so they can ...