Skip to content

Instantly share code, notes, and snippets.

@Ibro
Created March 25, 2017 12:50
Show Gist options
  • Save Ibro/581afd45f44c1fca7d1b29e9b931010f to your computer and use it in GitHub Desktop.
Save Ibro/581afd45f44c1fca7d1b29e9b931010f to your computer and use it in GitHub Desktop.
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