Created
March 25, 2017 12:50
-
-
Save Ibro/581afd45f44c1fca7d1b29e9b931010f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function log(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) { | |
console.log('target:', target); | |
console.log('propertyKey:', propertyKey); | |
console.log('descriptor:', descriptor); | |
// save a reference to the original method | |
let originalMethod = descriptor.value; | |
descriptor.value = function (...args: any[]) { | |
// pre | |
console.log('New method..'); | |
console.log('The method args are: ' + JSON.stringify(args)); | |
// run and store the result | |
let result = originalMethod.apply(this, args); | |
// post | |
console.log('The return value is: ' + result); | |
// return the result of the original method | |
return result; | |
}; | |
return descriptor; | |
} | |
class Test { | |
@log | |
public hello(name: string): string { | |
console.log('hello: '); | |
return `return ${name}`; | |
} | |
} | |
let test = new Test(); | |
test.hello('John'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment