mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
chore: add guidance on getters/setters to coding standards (#7977)
This commit is contained in:
@@ -154,6 +154,24 @@ be part of the user-facing API. This typically applies to symbols used in templa
|
||||
Additionally, the `@docs-private` JsDoc annotation can be used to hide any symbol from the public
|
||||
API docs.
|
||||
|
||||
|
||||
#### Getters and Setters
|
||||
* Avoid long or complex getters and setters. If the logic of an accessor would take more than
|
||||
three lines, introduce a new method to contain the logic.
|
||||
* A getter should immediately precede its corresponding setter.
|
||||
* Decorators such as `@Input` should be applied to the getter and not the setter.
|
||||
* Always use a `readonly` property instead of a getter (with no setter) when possible.
|
||||
```ts
|
||||
/** YES */
|
||||
readonly active: boolean;
|
||||
|
||||
/** NO */
|
||||
get active(): boolean {
|
||||
// Using a getter solely to make the property read-only.
|
||||
return this._active;
|
||||
}
|
||||
```
|
||||
|
||||
#### JsDoc comments
|
||||
|
||||
All public APIs must have user-facing comments. These are extracted and shown in the documentation
|
||||
|
||||
Reference in New Issue
Block a user