• 简单理解就是在原方法执行过程中,插入一些其他的功能,一般用于监控功能
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)
const beforeAop = () => {
    console.log('beforeAop')
}
const afterAop = () => {
    console.log('afterAop')
}

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

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

let newSay = say.before(beforeAop).after(afterAop)
newSay()

UlaHalal
29 声望0 粉丝

主要用于笔记整理