Created
August 4, 2015 12:14
-
-
Save sothmann/915b13fdce147e6e1a1e to your computer and use it in GitHub Desktop.
TypeScript Decorator - Log method calls
This file contains hidden or 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
function log(target, key, descriptor: PropertyDescriptor) { | |
var originalMethod = descriptor.value; | |
descriptor.value = function(...args: any[]) { | |
let functionName = key; | |
console.log(functionName + "(" + args.join(", ") + ")"); | |
let result = originalMethod.apply(this, args); | |
console.log("=> " + result); | |
return result; | |
}; | |
return descriptor; | |
} | |
class Person { | |
@log | |
greet(name: string): string { | |
return `hello ${name}`; | |
} | |
} | |
new Person().greet("world"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result is