- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 189字
- 2021-07-15 17:05:34
Method decorators
Method decorators are declared before the method declaration. This decorator is used to modify, observe, or replace a method definition and is applied to the property descriptor for the method. The following code snippet shows a simple class with an applied method decorator:
class Hello { @logging increment(n: number) { return n++; } }
The Hello class has the increment method that increments a number supplied to its parameter. Note that the increment method is decorated with the @logging decorator to log input and output of the increment method. The following is the code snippet of the logging function:
function logging(target: Object, key: string, value: any) { value.value = function (...args: any[]) { var result = value.apply(this, args); console.log(JSON.stringify(args)); return result; } }; }
The method decorator function takes three arguments: target, key, and value. target holds the method that is being decorated; key holds the name of the method being decorated; and value is the property descriptor of the specified property if it exists on the object.
The logging method gets invoked when the increment method is called and it logs the value to the console.
- Java范例大全
- R語言經(jīng)典實(shí)例(原書第2版)
- 老“碼”識途
- Rust Essentials(Second Edition)
- Swift Playgrounds少兒趣編程
- PyQt編程快速上手
- 軟件測試分析與實(shí)踐
- Python編程基礎(chǔ)教程
- Mastering OpenStack
- R語言實(shí)戰(zhàn)(第2版)
- Elasticsearch搜索引擎構(gòu)建入門與實(shí)戰(zhàn)
- 計(jì)算機(jī)系統(tǒng)解密:從理解計(jì)算機(jī)到編寫高效代碼
- LibGDX Game Development By Example
- Continuous Integration,Delivery,and Deployment
- Hands-On Game Development Patterns with Unity 2019