AOP-面向切片编程

12次阅读

共计 375 个字符,预计需要花费 1 分钟才能阅读完成。

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)

正文完
 0