function log(target: Function, key: string, value: any) { return { value: (...args: any[]) => { var a = args.map(a => JSON.stringify(a)).join(); var result = value.value.apply(this, args); // How to avoid hard coded this? var r = JSON.stringify(result); console.log(`Call: ${key}(${a}) => ${r}`); return result; } }; }
这个时候会提示Uncaught TypeError: this.bar is not a function. 这是由于log中使用了箭头函数,改变了方法的作用域。this对象指向的是当前函数的上下文,而不是调用log方法的this对象。这边不要使用箭头函数
1 2 3 4 5 6 7 8 9 10 11
function log(target: Object, key: string, value: any) { return { value: function(...args: any[]) { var a = args.map(a => JSON.stringify(a)).join(); var result = value.value.apply(this, args); var r = JSON.stringify(result); console.log(`Call: ${key}(${a}) => ${r}`); return result; } }; }
function log(target: Object, key: string, descriptor: TypedPropertyDescriptor<any>) { var originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) { var a = args.map(a => JSON.stringify(a)).join(); var result = originalMethod.apply(this, args); var r = JSON.stringify(result); console.log(`Call: ${key}(${a}) => ${r}`); return result; };