Reputation: 10624
I can see some examples on the internet that's using getter method.
something like this,
setId(id: number) {
this._id = id;
}
get id() {
return this._id;
}
What're the benefits using that? beside using getId()
Upvotes: 1
Views: 349
Reputation: 657761
The most common case is not the getter, that's rather a side effect, but the setter allows to execute code (valdiation or similar) when the value is updated.
A common getter example is also fullName
where you store first name and last name in two different fields and fullName
just returns ${this.firstName} ${this.lastName}
.
What you can do with getter and setter can be done with methods as well, but having properties with getters and setters that can be used like simple fields, where it's not obvious there is calculation going on seem more natural.
Upvotes: 6