JavaScript — Object Oriented Programming: Encapsulation / Abstraction

Keerthana
3 min readApr 14, 2022

--

An article for Mid level JavaScript Developers.
This is part of a series “JavaScript — Object Oriented Programming”

Series “JavaScript — Object Oriented Programming”

Encapsulation — Binding the data members with the member functions. It helps to control the data and validate it.
Abstraction — Hiding the implementation details and showing only the functionality to the users.

Encapsulation in Javascript

To achieve an encapsulation : -
— Make data members private.
— Use setter methods to set the data and getter methods to get that data.

Example:

createPerson has a private variable _name.
Underscore _ is only a convention. It doesn’t enforce rule.
Here _name can be modified only via its getters and setters i.e. data member is tightly bound to its member functions.

function createPerson(name) {
let _name = name;
return {
// Getters
getName() {
return _name;
},
// Setters
setName(newName) {
_name = newName;
}
};
}
const p1 = createPerson('Harvey');
const p2 = createPerson('Mike');
console.log(p1._name); // undefined
console.log(p1.getName()); // Harvey
console.log(p2.getName()); // Mike
p1._name = 'Donna';
console.log(p1.getName()); // Harvey
p1.setName('Louis');
console.log(p1.getName()); // Louis

console.dir(p1);
Below image shows how getName has a closure _name

Abstraction in Javascript

To achieve an abstraction : -
— Shouldn’t allow to create an instance of Abstract Class, but allow to inherit from it, resulting in reduced code.

Example:

We cannot create an instance of Abstract Class Vehicle, it will throw error.
But we are able to inherit from it.

//Creating a constructor function
function Vehicle() {
this.vehicleName = 'vehicleName';
throw new Error('You cannot create an instance');
}
Vehicle.prototype.display = function() {
return 'Vehicle is: ' + this.vehicleName;
};
//Creating a constructor function
function Bike(vehicleName) {
this.vehicleName = vehicleName;
}
//Creating object without using the function constructor
Bike.prototype = Object.create(Vehicle.prototype);
// var vehicle = new Vehicle();
// Throws Error
var bike = new Bike('Honda');
console.log(bike.display());
console.log("bike instanceof Vehicle - "+(bike instanceof Vehicle));
console.log("bike instanceof Bike - "+(bike instanceof Bike));

— Make data member function private. (Hiding the implementation details and showing only the functionality to the users.)

Example:

Below example has private variable privateVar and method privateMethod
Wrapped in constructor function but not prefixed with this keyword.

function TempClass(param) {
this.classMember = param;
var privateVar = 'privateVal';
function privateMethod() {
// private variables can be accessed here
return privateVar;
}
this.privilegedMethod = function() {
// private method can be accessed here
console.log(privateMethod());
return privateVar;
};
}
var myClass = new TempClass('value');

Other Topics in this Series

JavaScript — Object Oriented Programming

6 stories

Instances / Constructor Functions / Es6 Class
Prototype Property / Prototype Object / Prototype Chaining
Prototypal Inheritance / Class Inheritance
Public and Private Members
Static Variables

--

--

No responses yet