AOP 面向切片编程

AOP 面向切片编程

  • 简单理解就是在原方法执行过程当中,插入一些其余的功能,通常用于监控功能
const say = (a = 0, b = 0) => {
    console.log(`say~~~ a:${a}, b:${b}`)
}

say()
  • 如今需求在say方法执行以前再打印个东西(不改变源方法)
const beforeAop = () => {
    console.log('beforeAop')
}

Function.prototype.before = function (fn) {
    let that = this
    return function () {
        fn()
        that.apply(null, arguments)
    }
}

const say = (a = 0, b = 0) => {
    console.log(`say~~~ a:${a}, b:${b}`)
}

let newSay = say.before(beforeAop)
newSay(1, 2)
相关文章
相关标签/搜索