先把demo放这里,有时间再来整理。
class Node {
constructor(public x: number, public y: number) {}
@action
action() {
console.log('...', this)
return 'hello aop'
}
}
// 不能用箭头函数
function action(target: Object, key: string) {
// get origin
const origin = target[key]
// aop
target[key] = function(...args) {
// connect0
console.log('before')
// resolve origin apply this, 必须用apply
let result = origin.apply(this, args)
// connect1
console.log('after')
return result
}
return target[key]
}
console.log(new Node(1, 1).action())
/*
before
... { x: 1, y: 1 }
after
hello aop
*/