JavaScript Encapsulation
Encapsulation is an object-oriented programming concept that allows us to keep the data and behavior of an object together by protecting the internal state of an object from outside interference. Encapsulation provides a way to hide the complexity of an object’s functions from other parts of the application.
Syntax
class ClassName {
constructor() {
// private variable
var privateVar = 0;
// private method
var privateMethod = function() {
console.log('Private method');
}
// public method
this.publicMethod = function() {
console.log('Public method');
}
}
}
Example
Let’s consider an example of encapsulation in JavaScript where we will create a Person
class that has a private member age
. We will expose a public method getAge()
to get the value of age
and a private method setAge()
to set the value of age
.
class Person {
constructor() {
// private variable
var age = 0;
// private method
var setAge = function(newAge) {
age = newAge;
}
// public method
this.getAge = function() {
return age;
}
// public method
this.setAge = function(newAge) {
if(newAge > 0 && newAge < 120){
setAge(newAge);
} else{
console.log("Invalid Age");
}
}
}
}
Output
Let's create a Person
object and set the age using the setAge()
method and retrieve the age of the person using the getAge()
method.
const person = new Person();
person.setAge(30);
console.log(person.getAge()); // Output: 30
Explanation
In the above example, we have created a Person
class with a private member age
. We have also defined two public methods getAge()
and setAge()
. The setAge()
method is a private method that sets the value of age
and is only called from within the Person
class. It checks whether the value of age
is in the valid range before setting it. The getAge()
method is a public method that returns the value of age
to the caller.
By encapsulating age
within the Person
class, we have ensured that it can be accessed only through the getAge()
and setAge()
methods, which are public methods of the Person
class. This helps in maintaining the integrity of the Person
object’s data.
Use
The main use of encapsulation is to protect the internal state of an object from outside interference. Encapsulation helps to reduce complexity, increase security, and simplifies code maintenance.
Encapsulation is also useful in creating reusable code. By encapsulating the behavior of an object within it, we can reuse the object in different parts of the application without worrying about its internal details.
Important Points
- Encapsulation is a concept in object-oriented programming that allows us to keep the data and behavior of an object together by protecting the internal state of an object from outside interference.
- The private members of an object are only accessible from within the object.
- The public members of an object are accessible from outside the object.
- Encapsulation helps in reducing complexity, increasing security, and simplifying code maintenance.
- Encapsulation also helps in creating reusable code.
Summary
Encapsulation is an object-oriented programming concept that allows us to keep the data and behavior of an object together by protecting the internal state of an object from outside interference. Encapsulation helps in reducing complexity, increasing security, and simplifying code maintenance. By encapsulating the behavior of an object within it, we can reuse the object in different parts of the application without worrying about its internal details.