问题:如何让下面的代码执行输出 1 2 3
const app = () => {
console.log(2)
}
const start = app
.before(() => {
console.log(1)
})
.after(() => {
console.log(3)
})
start()
答案:
Function.prototype.after = function (afterFn) {
return (...args) => {
this.apply(this, args)
afterFn.apply(this, args)
}
}
Function.prototype.before = function (beforeFn) {
return (...args) => {
beforeFn.apply(this, args)
this.apply(this, args)
}
}