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)
相關文章
相關標籤/搜索