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;
}
Run Code Online (Sandbox Code Playgroud)
What're the benefits using that? beside using getId()
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}.
您也可以使用方法来完成getter和setter的操作,但是具有可以像简单字段一样使用的getter和setter的属性,在这种情况下,显而易见,进行计算似乎更为自然。
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |