Encapsulation and Abstraction

Encapsulation

Encapsulation is the practice of hiding the implementation details of an object from the outside world. This makes it easier to maintain and extend the object, and it also makes the object more secure.

In JavaScript, encapsulation is achieved by using access modifiers. Access modifiers control who can access the properties and methods of an object.

There are three access modifiers in JavaScript:

  • Public: Public properties and methods can be accessed from anywhere.
  • Private: Private properties and methods can only be accessed from within the object itself.
  • Protected: Protected properties and methods can be accessed from within the object itself and from its subclasses.

For example, the following code defines a constructor function called Person with a private property called name and a public method called greet():

function Person() { this.name = "John Doe"; }
Person.prototype.greet = function() { console.log("Hello, my name is " + this.name); };
// The name property is private, so it can only be accessed from within the Person object itself.
// The greet() method is public, so it can be accessed from anywhere.

Access Modifiers: Public, Private, and Protected

The following table summarizes the three access modifiers in JavaScript:

Access modifier

Description

Public

Public properties and methods can be accessed from anywhere.

Private

Private properties and methods can only be accessed from within the object itself.

Protected

Protected properties and methods can be accessed from within the object itself and from its subclasses.

Getters and Setters

Getters and setters are special methods that are used to get and set the value of a property. Getters are used to get the value of a property, and setters are used to set the value of a property.

Getters and setters are often used to encapsulate the value of a property. For example, the following code defines a constructor function called Person with a private property called age and a getter and setter for the age property:

function Person() { this.age = 30; }
Person.prototype.getAge = function() { return this.age; };
Person.prototype.setAge = function(newAge) { this.age = newAge; };
// The age property is private, so it can only be accessed through the getAge() and setAge() methods.

Creating Abstractions with Classes

In object-oriented programming, an abstraction is a way of representing something without having to expose all of its details. Abstractions can be used to make code more readable, maintainable, and reusable.

In JavaScript, classes can be used to create abstractions. A class is a blueprint for an object. It defines the properties and methods of an object.

For example, the following code defines a class called Person:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log("Hello, my name is " + this.name);
  }
}

The Person class defines two properties: name and age. It also defines a method called greet().

The Person class can be used to create objects. For example, the following code creates a new Person object:

var person = new Person("John Doe", 30);

The person variable now refers to a new Person object that has the name property set to “John Doe” and the age property set to 30.

Conclusion

Encapsulation, access modifiers, getters and setters, and classes are all important concepts in object-oriented programming in JavaScript. By understanding these concepts, you can create powerful and reusable objects.