- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 192字
- 2021-07-15 17:05:34
Class decorators
Class decorators are declared above the class declaration. Class decorators can observe, modify, and replace a class' definition that it is decorated by applying to the constructor of that class. The signature of ClassDecorator in TypeScript is as follows:
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
Consider a Customer class; we would like that class to be frozen. Its existing properties should not be removed or new properties should not be added.
We can create a separate class that can take any object and freeze it. We can then decorate the customer class with @freezed to prevent adding new properties or removing the existing properties from the class:
@freezed class Customer { public firstName: string; public lastName: string; constructor(firstName : string, lastName : string) { this.firstName = firstName; this.lastName = lastName; } }
The preceding class takes four arguments in the firstname and lastname constructors. The following are the code snippets of the function written for the @freezed decorator:
function freezed(target: any) { Object.freeze(target); }
Here, the freezed decorator takes target, which is the Customer class that is being decorated, and freezes it when it gets executed.
- Vue.js設計與實現(xiàn)
- CMDB分步構建指南
- Developing Middleware in Java EE 8
- Mastering QGIS
- Python Deep Learning
- 秒懂設計模式
- 好好學Java:從零基礎到項目實戰(zhàn)
- Hands-On Kubernetes on Windows
- QPanda量子計算編程
- UX Design for Mobile
- Oracle Database XE 11gR2 Jump Start Guide
- Elasticsearch搜索引擎構建入門與實戰(zhàn)
- Learning WordPress REST API
- Learning Swift
- Python AI游戲編程入門:基于Pygame和PyTorch