Here we mainly look at the property accessor, that is, the get, set method.
The Chinese names for getter (reading) and setter (writing) are accessor, memory. As the name suggests, it is used to obtain and set variables. Having read about other object-oriented languages (C#, Java) for getters, setters, the advantages are summarized here:
1. For object-oriented languages, directly exposing member variables is not in line with the encapsulation principle of OOP, and it is not safe, so getters and setters should be used to take and assign values.
2. These two methods can be used to add additional functions (such as conditional filtering, verification), etc.
3. Internal storage and external performance are different.
4. The internal storage method and logic can be modified while the external interface remains unchanged.
5. Manage the lifecycle and memory storage method of arbitrary management variables.
6. Provide a debug interface.
7. It can be integrated with simulated objects, serialization, and even WPF libraries.
8. Allow inheritors to change semantics.
9. You can use getters and setters for lambda expressions. (Presumably as a function, participating in function transmission and operation)
10. Getters and setters can have different access levels.
In C#, we use accessors in a way that
In TypeScript, it is used as follows:
Although it is more troublesome to write, it is still very convenient to use.
|