JavaScript Object Accessors
JavaScript Getters and Setters
Getters and setters allow you to define Object Accessors (Computed Properties).
JavaScript Getter (The get Keyword)
This example uses a lang property to get
the value of the language property.
Example
// Create an object:
const person = {
firstName: "John",
lastName: "Doe",
language: "en",
get lang() {
return this.language;
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
Try it Yourself »
JavaScript Setter (The set Keyword)
This example uses a lang property to set
the value of the language property.
Example
const person = {
firstName: "John",
lastName: "Doe",
language: "",
set lang(lang) {
this.language = lang;
}
};
// Set an object
property using a setter:
person.lang = "en";
// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
Try it Yourself »
JavaScript Function or Getter?
What is the difference between these two examples?
Example 1
const person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " +
this.lastName;
}
};
// Display data from the object using a method:
document.getElementById("demo").innerHTML = person.fullName();
Try it Yourself »
Example 2
const person = {
firstName: "John",
lastName: "Doe",
get fullName() {
return this.firstName + " " +
this.lastName;
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.fullName;
Try it Yourself »
Example 1 accesses fullName as a function: person.fullName().
Example 2 accesses fullName as a property: person.fullName.
The second example provides a simpler syntax.
Data Quality
JavaScript can secure better data quality when using getters and setters.
Using the lang property, in this example, returns the value
of the language property in upper case:
Example
// Create an object:
const person = {
firstName: "John",
lastName: "Doe",
language: "en",
get lang() {
return this.language.toUpperCase();
}
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
Try it Yourself »
Using the lang property, in this example, stores an upper case
value in the language property:
Example
const person = {
firstName: "John",
lastName: "Doe",
language: "",
set lang(lang) {
this.language = lang.toUpperCase();
}
};
// Set an object
property using a setter:
person.lang = "en";
// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
Try it Yourself »
Why Use Getters and Setters?
- It gives simpler syntax
- It allows equal syntax for properties and methods
- It can secure better data quality
- It is useful for doing things behind-the-scenes
Avoid getters or setters if you only need simple values (prevents overuse).
Object.defineProperty()
The Object.defineProperty() method can also be used to add Getters and
Setters:
A Counter Example
The getter only reads the value. The setter changes it, and ignores negative numbers.
// Define object
const obj = {_count : 0};
// Define a getter and a setter
Object.defineProperty(obj, "count", {
get : function () {return this._count;},
set : function (value) {
if (value >= 0) {this._count = value;}
}
});
// Play with the counter:
obj.count = 5;
obj.count = obj.count + 1;
obj.count = -3;
let text = obj.count;
Try it Yourself »