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.
If you want to learn more, you can check
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 thePerson
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 forname
andage
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.
Controlling Access and Modification of Data
Imagine we are creating a class called BankAccount
, where we want to control access and assignment of the balance.
We want to allow the balance to be accessible, but only modifiable if the amount to withdraw is valid (i.e., not exceeding the current balance).
class BankAccount {
constructor(balance) {
this._balance = balance;
}
// Get method to access the balance
get balance() {
return this._balance;
}
// Set method to modify the balance
set balance(newBalance) {
if (newBalance >= 0) {
this._balance = newBalance;
} else {
console.log("The balance cannot be negative");
}
}
// Method to make a withdrawal
withdraw(amount) {
if (amount <= this._balance) {
this._balance -= amount;
console.log(`Withdrawal successful. Remaining balance: ${this._balance}`);
} else {
console.log("Insufficient funds");
}
}
}
const account = new BankAccount(1000);
console.log(account.balance); // Access through the getter: 1000
account.withdraw(500); // Makes a valid withdrawal
console.log(account.balance); // Remaining balance: 500
account.balance = -100; // Attempts to assign a negative balance
In this example:
get balance()
: Allows access to the account balance without allowing it to be modified directly from outside the class.set balance()
: Allows assigning a new balance to the account, but validates that the value is not negative.withdraw(amount)
: Performs a validation to ensure that the withdrawal amount is not greater than the available balance.
Computed Properties
A common case is when we want a property to be calculated from other properties (we call them computed properties).
For example, let’s imagine a class that calculates the area of a circle based on its radius. We can use a get
method to access the area, and a set
method to modify the radius.
class Circle {
#radius;
constructor(radius) {
this.#radius = radius;
}
// Set method to establish the radius
set radius(newRadius) {
if (newRadius > 0) {
this.#radius = newRadius;
} else {
console.log("The radius must be a positive value");
}
}
// Get method to calculate the area
get area() {
return Math.PI * this.#radius ** 2;
}
}
const circle = new Circle(5);
console.log(circle.area); // Shows the area (approximately 78.54)
circle.radius = 10; // Changes the radius
console.log(circle.area); // Shows the new area (approximately 314.16)
Here,
- The
get
method forarea
dynamically calculates the area based on the current value ofradius
. - It is not necessary to store the area as a property, as it is calculated automatically (computed property).