cover-img

Accessor Object Properties in JavaScript

20 January, 2023

1

1

0

How to define Accessor Properties

In JavaScript, the get and set keywords are used to define accessor properties. Accessor properties use getter and setter methods to define how a property is accessed and modified.

The get keyword defines a getter method, a function called when the property is accessed. A getter method typically returns the value of the property.

The set keyword defines a setter method, a function called when the property is modified. A setter method typically updates the property's value with the value passed to it.

Accessor properties are defined using the Object.defineProperty() method and are denoted by the get and set keywords.

Here's an example of how to define an accessor property:

let person = {
_name: "John",
get name() {
return this._name;
},
set name(value) {
this._name = value;
}
};
console.log(person.name); // Output: "John" (invokes the getter method)
person.name = "Jane"; // invokes the setter method
console.log(person.name); // Output: "Jane"

In this example, the name property is defined as an accessor property with a getter method that returns the value of the *name property and a setter method that updates the value of* name property.

1

1

0

ShowwcaseHQ
Showwcase is where developers hang out and find new opportunities together as a community

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.