JavaScript Getter And Setter

JavaScript getters and setters are special methods that provide access to object properties. Getters are used to read values of properties, while setters are used to write values to properties. This tutorial will show you how to use getters and setters in JavaScript.

Why should you use it?

  • Getters and setters allow you to control how your object properties are accessed and modified.
  • Getters and setters can be used to validate data before it is set to an object.
  • Getters and setters can be used to create properties that are calculated on the fly.

Getter

Getters are functions that are used to access properties on an object. They are similar to methods, but they are not called on an instance of an object. Instead, they are called on the object itself. Getters are often used to access values from an object that are not directly accessible, such as nested properties.

Getters are defined using the get keyword, followed by a function. This function takes no arguments and returns a value. The value returned can be any valid JavaScript value, including objects and arrays.

The following example shows a getter defined on an object called person. The getter is called name, and it returns the value of the name property on the object:

let person = {
  name: 'John',
  get name() {
    return this.name;
  }
};

To access the value of the name property, we can call the getter like this:

person.name

The result of this code will be the value of the name property, which is "John".

We can also use the getter to set the value of the name property:

let person = {
  name: 'John',
  get name() {
    return this.name;
  },
  set name(value) {
    this.name = value;
  }
};

person.name = 'Jane';

Now, the value of the name property will be "Jane".

To learn more about getter, visit Getter.

Setter

The setter is a method that allows you to set the value of a property in an object. It is a special type of method that is used to set the value of a property in an object. The setter method takes one argument, which is the value that you want to set the property to. The setter method is defined with the keyword "set".

For example, let's say we have an object called "user" with a property called "name". We can define a setter method for the "name" property like this:

const user = {
  name: '',
  set name(value) {
    this.name = value;
  },
};
      

Now, whenever we try to set the value of the "name" property, the setter method will be called and the value will be set to the value that we passed in. For example:

const user = {
  name: '',
  set name(value) {
    this.name = value;
  },
};

user.name = 'John';
console.log(user.name); // John
      

The setter method is a great way to ensure that the value of a property is always set correctly. For example, if we want to make sure that the value of the "name" property is always a string, we can use a setter method to check the type of the value and throw an error if it is not a string:

const user = {
  name: '',
  set name(value) {
    if (typeof value !== 'string') {
      throw new Error('Name must be a string');
    }
    this.name = value;
  },
};

user.name = 123; // Error: Name must be a string
      

The setter method is also a great way to ensure that the value of a property is always up to date. For example, if we want to make sure that the value of the "name" property is always the same as the value of the "fullName" property, we can use a setter method to update the value of the "name" property whenever the "fullName" property is changed:

const user = {
  name: '',
  fullName: '',
  set name(value) {
    this.name = this.fullName;
  },
  set fullName(value) {
    this.fullName = value;
    this.name = value;
  },
};

user.fullName = 'John Smith';
console.log(user.name); // John Smith
      

Setter methods are a great way to make sure that the values of properties are always set correctly and are always up to date. They are a powerful tool for ensuring that your code is always running correctly.

To learn more about setter, visit Setter.

1