Abstraction in JavaScript
In this tutorial, we will learn about abstraction in JavaScript.
Abstraction is one of the four fundamental OOP concepts. It is the process of hiding the implementation details and showing only the essential features of an object. It allows developers to focus on what an object does instead of how it does it. It allows developers to create a simple interface for complex systems.
JavaScript does not have built-in support for abstraction, but developers can use closures and prototypes to implement abstraction.
Example
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getName = function () {
return this.name;
};
let person = new Person("John", 25);
console.log(person.name); // John
console.log(person.age); // 25
console.log(person.getName()); // JohnIn the above example, the Person class has a name and age property and a getName method. The name and age properties are public properties that can be accessed from outside the object. The getName method is a public method that can be accessed from outside the object.
Conclusion
- Abstraction is the process of hiding the implementation details and showing only the essential features of an object.
- JavaScript does not have built-in support for abstraction, but developers can use closures and prototypes to implement abstraction.
- Abstraction allows developers to focus on what an object does instead of how it does it.
- Abstraction allows developers to create a simple interface for complex systems.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on