js通过拦截器实现链式操作
2019-12-09 本文已影响0人
sorry510
const pipe = (function() {
return value=> {
const funcStack = []
const oproxy = new Proxy({}, {
get(pipeObject, fnName) {
if(fnName === 'get') {
return funcStack.reduce((val, fn)=> fn(val), value)
}
funcStack.push(window[fnName])
return oproxy
}
})
return oproxy
}
})()
var add5 = a => a+5
var pow = n => n * n
pipe(3).add5.pow.get // 64