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

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.

主站蜘蛛池模板: 正宁县| 安多县| 南平市| 凤庆县| 汉寿县| 铁力市| 桂东县| 湘潭县| 黑龙江省| 四会市| 赤壁市| 祥云县| 成都市| 蒲江县| 封丘县| 罗山县| 西吉县| 博白县| 九台市| 湟源县| 广宁县| 华阴市| 汨罗市| 临湘市| 昌黎县| 东兰县| 揭东县| 二手房| 陆河县| 荣昌县| 陆河县| 通辽市| 茂名市| 当雄县| SHOW| 张北县| 格尔木市| 塘沽区| 凉山| 大关县| 怀柔区|