xxxxxxxxxx
// 1) In JavaScript, getter methods are used to access the properties of an object.
// we use get keyword to define a getter method to get or access the property value
const student = {
// data property
firstName: 'Chetan',
// accessor property(getter)
get getName() {
return this.firstName;
}
};
// accessing data property
console.log(student.firstName); // Chetan
// accessing getter methods or accessing the value as a property
console.log(student.getName); // Chetan
// trying to access as a method
console.log(student.getName()); // error
// In the above program, a getter method getName() is created to access the property of an object.
// Note: To create a getter method, the get keyword is used.
// 2) In JavaScript, setter methods are used to change the values of an object.
// we use set keyword to define a setter method to set the property value
const student = {
firstName: 'Chetan',
//accessor property(setter)
set changeName(newName) {
this.firstName = newName;
}
};
console.log(student.firstName); // Chetan
// change(set) object property using a setter
student.changeName = 'Jeny';
console.log(student.firstName); // Jeny
// In the above example, the setter method is used to change the value of an object.
// Note: To create a setter method, the set keyword is used.
xxxxxxxxxx
// Create an object:
const person = {
firstName: "Md",
lastName: "Raquib",
language: "",
//Your getter, used for getting the full name
get fullName() {
return this.firstName + " " + this.lastName;
},
//Your setter, used for changing the value of language property
set lang(lang) {
this.language = lang;
}
};
console.log(person.fullName);//logs "Md Raquib" (using Getter)
person.lang = "english";//We set the language as "english" (using Setter)
console.log(person.language);//logs "english"
xxxxxxxxxx
// although you can just grab a property directly like this
Car1.colour
// or modify it like this
Car1.colour = "red"
// on the other hand, getter methods look like this
get colour() { // doesn't need a parameter
return this.colour // optionally attach values or operations on to it
}
// and setter methods look like this
set colour(newColour) {
this.colour = newColour
}
//Benifits of getters and setters
// implement logic that uses private data in the class
// eg. I might want to blend it with the current car colour
// Encapsulation: allows you to control how the data is accessed or modified.
// especially if you are implementing other logic
// Readability: code is much clearer as it is now in a method
// Compatibility: Some APIs or libraries expect you to use getters/setters
xxxxxxxxxx
let obj = {
log: ['a', 'b', 'c'],
get latest() {
if (this.log.length === 0) {
return undefined;
}
return this.log[this.log.length - 1];
}
};
obj.log.push('d');
console.log(obj.latest); //output: 'd'
xxxxxxxxxx
class Person {
constructor(name) {
this.name = name;
}
// getter
get personName() {
return this.name;
}
// setter
set personName(x) {
this.name = x;
}
}
let person1 = new Person('Jack');
console.log(person1.name); // Jack
// changing the value of name property
person1.personName = 'Sarah';
console.log(person1.name); // Sarah