AOP切片编程

2019-01-03 js编程思想

  Function.prototype.before = function (callback) {
    let self = this
    return function (...args) {
      callback.call(self, ...args)
      return self.call(self, ...args)
    }
  }
  Function.prototype.after = function (callback) {
    let self = this
    return function (...args) {
      self.call(self, ...args)
      callback.call(self, ...args)
    }
  }
  let fn = function test() {
    console.log('pending');
  } 
  fn.before(() => {
    console.log('before');
  })
  .after(() => {
    console.log('after');
  })()


最后更新于: 2022年11月16日 19:31