Language: EN

set-y-get-en-clases-de-javascript

Set and Get Methods in JavaScript

The get and set methods are a special syntax for declaring methods that allow you to declare methods that are used with a syntax like variables.

Getter and setter methods are functions whose main purpose is to access a property of an object, but performing certain functions intended to maintain the internal state of the object.

The get and set syntax allows you to define getter and setter methods more conveniently, making it easier to encapsulate and validate data within a class.

Get Methods

In general terms, a getter method is a special function that allows you to access a property of an object.

If we use the word get before a method, we are indicating that we want it to be a getter.

Now you can get the value of a get function as if it were a variable, but internally you are executing a method every time you get the value.

Let’s see it with an example.

class Person {
	#name;
	#age;
	
    constructor(name, age) {
        this.#name = name;  // Internal property
        this.#age = age;    // Internal property
    }

    get name() {
        return this.#name;   // Controlled access to the #name property
    }

    get age() {
        return this.#age;    // Controlled access to the #age property
    }
}

const person1 = new Person("Luis", 25);
console.log(person1.name);  // Calls the get method for name and shows "Luis"

In the previous example,

  • The get name method allows controlled access to the private #name property of the Person class.
  • Externally, it is used almost as if it were a variable using dot notation, with person1.name.

Set Methods

On the other hand, a setter method is a special function that allows assigning a property of an object.

The main advantage of set methods is that they allow you to validate and modify values before they are assigned to properties.

Let’s see it with a simple example.

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

    set name(newName) {
        if (newName.length > 0) {
            this.#name = newName;  // Validation before assigning the value
        } else {
            console.log("The name cannot be empty");
        }
    }

    set age(newAge) {
        if (newAge > 0) {
            this.#age = newAge;  // Validation before assigning the value
        } else {
            console.log("Age must be a positive value");
        }
    }
}

const person2 = new Person("Luis", 30);
person2.name = "";  // Attempt to assign an empty name
person2.age = -5;    // Attempt to assign a negative age
console.log(person2.name);  // Shows "Luis" (not changed)
console.log(person2.age);    // Shows 30 (not changed)

In this example,

  • The set methods for name and age allow validation before modifying the internal properties of the object.
  • If the values are not valid, the set method can block the assignment.

Practical Examples

Let’s look at some practical examples of how to use these methods in JavaScript classes.