This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13565|Reply: 0

[TypeScript] TypeScript property accessor (set,get)

[Copy link]
Posted on 5/27/2020 2:51:03 PM | | |
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.





Previous:js string inversion
Next:Detailed explanation of temporary tables in SQL server databases
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com