Getters and Setters in JavaScript Classes

Getters and Setters in JavaScript Classes

·

4 min read

Looking to add an extra layer of security and control to the data in your objects? Getters and setters are the answer! These methods are used to get and set the values of properties in an object, respectively. They're often used in conjunction with each other to validate data before setting it.

What are Getters and Setters?

Getters and setters are methods that are used to get and set the values of properties in an object, respectively. They are usually used in conjunction with each other, and are often used to validate data before setting it.

Why use Getters and Setters?

Getters and setters are used to protect the data in an object from being modified in an uncontrolled way. By using getters and setters, you can make sure that the data in your object is always valid and consistent.

How to use Getters and Setters

To use getters and setters, you need to define them in your class. Getters and setters are defined like any other method, but they have a special syntax.

Getters are defined by prefixing the method name with get. Setters are defined by prefixing the method name with set.

Here is an example class with a getter and a setter:

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

  get name() {
    return this._name;
  }

  set name(name) {
    this._name = name;
  }

  get age() {
    return this._age;
  }

  set age(age) {
    if (age >= 0 && age <= 100) {
      this._age = age;
    } else {
      throw new Error('Invalid age');
    }
  }
}

As you can see, the getters and setters have the same name as the property they are getting or setting.

To use a getter, you simply call the method with the get keyword:

const person = new Person('John', 30);
console.log(person.name); // John

To use a setter, you call the method with the set keyword and pass in the value you want to set:

const person = new Person('John', 30);
person.age = 40;
console.log(person.age); // 40

Getters and Setters vs. Public Properties

Getters and setters offer a more controlled way to access and modify the data in an object than public properties. With public properties, anyone can access and modify the data without any restrictions.

For example, consider the following class:

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

This class has two public properties: name and age. This means that anyone can access and modify these properties:

const person = new Person('John', 30);
person.name = 'Jane'; // This is perfectly valid
person.age = -10; // This is also perfectly valid

With getters and setters, you can restrict how the data in your object can be accessed and modified. For example, you could add validation to the age setter to make sure that the age is always a valid value:

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

  get name() {
    return this._name;
  }

  set name(name) {
    this._name = name;
  }

  get age() {
    return this._age;
  }

  set age(age) {
    if (age >= 0 && age <= 100) {
      this._age = age;
    } else {
      throw new Error('Invalid age');
    }
  }
}

Now, if someone tries to set an invalid age, they will get an error:

const person = new Person('John', 30);
person.age = -10; // This will throw an error

Getters and Setters vs. Public Methods

Getters and setters offer a more controlled way to access and modify the data in an object than public methods. With public methods, anyone can call the method and modify the data without any restrictions.

For example, consider the following class:

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

  setName(name) {
    this._name = name;
  }

  setAge(age) {
    this._age = age;
  }
}

Conclusion

Getters and setters offer a more controlled and flexible way to access and modify the data in an object. They should be used whenever you need to validate or transform data before setting it.

Did you find this article valuable?

Support trav by becoming a sponsor. Any amount is appreciated!