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)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理