In JavaScript, encapsulation can be achieved through a combination of closures and object-oriented programming techniques. Encapsulation is the concept of bundling the data (attributes) and the methods (functions) that operate on the data into a single unit, which is an object. It also involves controlling access to the internal state of an object to ensure data integrity. Here's how you can achieve encapsulation in JavaScript:
Private Properties and Methods:
JavaScript doesn't have native support for access control keywords like
private, public, or protected as some other languages do. However, you can emulate private properties and methods using closures.
Define variables or functions within the constructor or a factory function, and they will be accessible only within that scope.
function Person(name) {
let _name = name; // Private variable
function _getFullName() { // Private method
return _name;
}
this.sayHello = function() {
console.log(`Hello, my name is ${_getFullName()}`);
};
}
const person = new Person("Alice");
console.log(person._name); // undefined (private variable)
person.sayHello(); // Outputs: "Hello, my name is Alice"
Getter and Setter Methods:
Use getter and setter methods to access and modify private properties while controlling their access.
This allows you to validate and manage the data before it's read or modified.
With the introduction of ES6 classes, you can achieve encapsulation by defining private properties and methods using the
_ convention.
While these properties and methods are not truly private, the _ convention signals to other developers that they are intended to be treated as private.
class Rectangle {
constructor(width, height) {
this._width = width; // Private property
this._height = height; // Private property
}
area() {
return this._width * this._height; // Public method
}
}
const rect = new Rectangle(5, 10);
console.log(rect._width); // 5 (though it's meant to be private)
console.log(rect.area()); // Outputs: 50
Remember that JavaScript does not provide strict access control like some other languages, so encapsulation in JavaScript relies on conventions and the use of closures and getter/setter methods to create private and controlled access to properties and methods.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript, encapsulation can be achieved through a combination of closures and object-oriented programming techniques. Encapsulation is the concept of bundling the data (attributes) and the methods (functions) that operate on the data into a single unit, which is an object. It also involves controlling access to the internal state of an object to ensure data integrity. Here's how you can achieve encapsulation in JavaScript:
Private Properties and Methods:
Getter and Setter Methods:
Use ES6 Classes:
Remember that JavaScript does not provide strict access control like some other languages, so encapsulation in JavaScript relies on conventions and the use of closures and getter/setter methods to create private and controlled access to properties and methods.