前端js面向切面编程AOP

5次阅读

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

//Aop 构造器
function Aop(options){this.options = options}
// 业务方法执行前钩子
Aop.prototype.before = function(cb){cb.apply(this)
}
// 业务方法执行后钩子
Aop.prototype.after = function(cb){cb.apply(this)
}
// 业务方法执行器
Aop.prototype.execute = function(beforeCb,runner,afterCb){this.before(beforeCb)
    runner.apply(this)
    this.after(afterCb)
}

var aop = new Aop({
    afterInfo:'执行后',
    runnerInfo:'执行中',
    beforeInfo:'执行前'
})

var beforeCb = function(){console.log(this.options.beforeInfo)
}
var afterCb = function(){console.log(this.options.afterInfo)
}
var runnerCb = function(){console.log(this.options.runnerInfo)
}

aop.execute(beforeCb,runnerCb,afterCb)

正文完
 0