According to Martin, this syntax can be used to define a class in nodeJS:
class Person {
constructor() {
this.id = 'id_1';
}
set name(name) {
this._name = name;
}
get name() {
return this._name;
}
usage:
myPerson.name = "Saul"; // save the value in `_name` variable.
Slight problem with this convention is that calling JSON.stringify(myPerson)
will print the following:
{
"_name": "Saul"
}
But if I remove the underline from the setter function, and write it as the following:
set name(name) {
this.name = name;
}
the setter function will recursively call itself forever. So is there no way of using this nice getter-setter syntax, and still use the name
property? I'm running nodejs.