2022-02-04 16:28:24 +00:00

41 lines
1.1 KiB
Plaintext

``++readonly++`` properties can only be assigned in a class constructor or at the point of declaration. If a class has a property that's not marked ``++readonly++`` but is only set in the constructor, it could cause confusion about the property's intended use. To avoid confusion, such properties should be marked ``++readonly++`` to make their intended use explicit, and to prevent future maintainers from inadvertently changing their use.
== Noncompliant Code Example
[source,javascript]
----
class Person {
private _birthYear: number; // Noncompliant
constructor(birthYear: number) {
this._birthYear = birthYear;
}
}
----
== Compliant Solution
[source,javascript]
----
class Person {
private readonly _birthYear: number;
constructor(birthYear: number) {
this._birthYear = birthYear;
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]