JavaScript Getters and Setters
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
SAMER SAEID