官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 新津县| 古交市| 博湖县| 桃园市| 石屏县| 仁怀市| 山东省| 阿坝| 乐陵市| 汪清县| 佛教| 余庆县| 武川县| 宁远县| 桦甸市| 安岳县| 天长市| 青海省| 杭锦后旗| 琼结县| 磐石市| 济阳县| 天祝| 平潭县| 洪江市| 梧州市| 昭苏县| 库尔勒市| 迁西县| 乌兰察布市| 隆子县| 尉犁县| 青海省| 滦平县| 方山县| 怀安县| 沈阳市| 屯昌县| 鹿邑县| 安徽省| 福州市|