Getters and Setters in JS

In JavaScript, getters and setters are special methods that allow you to define how properties of an object are accessed and modified. They provide a way to control access to object properties, allowing you to add logic when getting or setting a value.

  • Getter: A getter is a method that gets the value of a specific property. It is defined using the get keyword.
  • Setter: A setter is a method that sets the value of a specific property. It is defined using the set keyword.

Here’s how you can use getters and setters within a class:

class Circle {
  constructor(radius) {
    this._radius = radius; // Use a different name for the actual property
  }

  // Getter for the radius property
  get radius() {
    return this._radius;
  }

  // Setter for the radius property
  set radius(value) {
    if (value >= 0) {
      this._radius = value;
    } else {
      console.error("Radius cannot be negative");
    }
  }

  get area() {
    return Math.PI * this._radius * this._radius;
  }
}

const myCircle = new Circle(10);
console.log(myCircle.radius); // Output: 10 (using the getter)

myCircle.radius = 15; // Output: (using the setter)
console.log(myCircle.radius);  // 15

myCircle.radius = -5;    // Error: Radius cannot be negative
console.log(myCircle.radius);  // 15 (the value is not changed)

console.log(myCircle.area); // 706.858 (calculated property)

In this example:

  • We use _radius to store the actual radius value.
  • The get radius() method returns the value of _radius.
  • The set radius(value) method checks if the provided value is valid (non-negative) before updating _radius. If the value is negative, it logs an error.
  • The get area() method calculates the area of the circle. Note that area is a read-only property, as it only has a getter and no setter.

** The underscore in this._radius is a convention used to indicate that the _radius property is intended to be treated as a private property. Here’s why:

Distinction from Public API: JavaScript doesn’t have true private properties like some other languages (e.g., Java). The underscore is a way for developers to signal, “Hey, this property is meant for internal use within the class. You should generally access and modify it through the provided getter and setter methods.”

Avoiding Naming Conflicts: Using _radius avoids a naming conflict with the public radius property that is exposed through the getter and setter. If we named the internal variable radius, we’d have a naming collision.

Encapsulation ( намек ): While not enforced by the language, this convention promotes encapsulation. Encapsulation is the idea of bundling data (properties) and methods that operate on that data within a single unit (a class), and restricting access to the internal details of the object.

In summary, the underscore is a helpful convention in JavaScript to improve code readability and намек at intended privacy, especially in the absence of true private properties.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *