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

Parameter decorators

Parameter decorators are prefixed to parameter declarations, and they are applied to a function for a class constructor or a method declaration. The signature of ParameterDecorator is as follows:

declare type ParameterDecorator = (target: Object, propertyKey:  
   string | symbol, parameterIndex: number) => void; 

Now, let's define the Customer class and use a parameter decorator to decorate a parameter in order to make it required and validate whether the value has been served:

class Customer { 
    constructor() {  } 
  
    getName(@logging name: string) { 
        return name; 
   } 
} 

Here, the name parameter has been decorated with @logging. The parameter decorator implicitly takes three inputs, namely prototype of the class that has this decorator, the name of the method that has this decorator, and the index of the parameter that is being decorated. The logging function implementation of the parameter decorator is as follows:

function logging(target: any, key : string, index : number) { 
  
  console.log(target); 
  console.log(key); 
  console.log(index); 
} 

Here, target is the class that has the decorator, key is the function name, and index contains the parameter index. This code just logs target, key, and index to the console.

主站蜘蛛池模板: 响水县| 方山县| 南华县| 伊吾县| 阿城市| 抚州市| 西藏| 汪清县| 乐东| 九台市| 大竹县| 福贡县| 白沙| 山阴县| 晋江市| 商城县| 嘉鱼县| 乐山市| 怀化市| 宁德市| 健康| 库伦旗| 法库县| 修武县| 宁阳县| 缙云县| 南康市| 馆陶县| 错那县| 苍山县| 桃园县| 黔西县| 溧阳市| 基隆市| 重庆市| 隆昌县| 阿鲁科尔沁旗| 仙居县| 襄垣县| 饶河县| 凤庆县|