- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 142字
- 2021-07-15 17:05:34
Accessor decorators
Accessor decorators are prefixed before the accessor declaration. These decorators are used to observe, modify, or replace an accessor definition and are applied to the property descriptor. The following code snippet shows a simple class with the applied accessor decorator applied:
class Customer { private _firstname: string; private _lastname: string; constructor(firstname: string, lastname: string) { this._firstname = firstname; this._lastname = lastname; } @logging(false) get firstname() { return this._firstname; } @logging(false) get lastname() { return this._lastname; } }
In this class, we decorate the get accessor of firstname and lastname with @logging and pass boolean to enable or disable logging. The following code snippet shows the function for the @logging decorator:
function logging(value: boolean) { return function (target: any, propertyKey: string, descriptor:
PropertyDescriptor) { descriptor.logging = value; }; }
The logging function sets the Boolean value to the logging property descriptor.
推薦閱讀
- Bootstrap Site Blueprints Volume II
- Twilio Best Practices
- YARN Essentials
- 零基礎學Java(第4版)
- 假如C語言是我發明的:講給孩子聽的大師編程課
- Python Data Analysis Cookbook
- 匯編語言編程基礎:基于LoongArch
- HoloLens與混合現實開發
- Hands-On Full Stack Development with Spring Boot 2.0 and React
- Practical Microservices
- Scrapy網絡爬蟲實戰
- Learning Alfresco Web Scripts
- Mastering Python
- AI輔助編程Python實戰:基于GitHub Copilot和ChatGPT
- Python編程:從入門到實踐(第2版)