Skip to content

get and set

Getter and setter functions allow computed or validated access to a property. They are defined with get and set in the descriptor.

const person = {
firstName: "John",
lastName: "Doe",
};
Object.defineProperty(person, "fullName", {
get() {
return `${this.firstName} ${this.lastName}`;
},
set(value) {
[this.firstName, this.lastName] = value.split(" ");
},
});
console.log(person.fullName); // John Doe
person.fullName = "Jane Smith";
console.log(person.firstName); // Jane