- 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.
- Visual C++實例精通
- R語言游戲數(shù)據(jù)分析與挖掘
- 軟件測試工程師面試秘籍
- R語言編程指南
- Servlet/JSP深入詳解
- Visual Basic程序設(shè)計習(xí)題解答與上機(jī)指導(dǎo)
- 深入淺出DPDK
- ASP.NET 3.5程序設(shè)計與項目實踐
- 深入分布式緩存:從原理到實踐
- 第一行代碼 C語言(視頻講解版)
- Service Mesh實戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實踐
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)APP設(shè)計
- 小程序從0到1:微信全棧工程師一本通
- JQuery風(fēng)暴:完美用戶體驗
- JavaScript從入門到精通(視頻實戰(zhàn)版)