Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Created January 19, 2016 22:02
Show Gist options
  • Save frontdevops/0691582f88bade60b18a to your computer and use it in GitHub Desktop.
Save frontdevops/0691582f88bade60b18a to your computer and use it in GitHub Desktop.
function log<T>(target: T, key :string, descriptor :any): T {
var originalMethod = descriptor.value;
descriptor.value = function (...args :any[]) :any {
let result = originalMethod.apply(this, args);
console.log(`Call: ${key}(${args.map(a=>JSON.stringify(a)).join()}) => ${JSON.stringify(result)}`);
return result;
};
return descriptor;
}
class Coffee {
protected cost :number = 1.2;
@log getCost() :number { return this.cost }
@log someDo() { return 'some text' }
}
let myCoffee = new Coffee;
let cost = myCoffee.getCost();
myCoffee.someDo();
/*
Output:
Call: getCost() => 1.2
Call: someDo() => "some text"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment