JavaScript/链式操作
来自康健生活
引言
Proxy
const pipe = (function() {
return (value, target) => {
const cup = [];
const tmp = new Proxy({}, {
get(_, name) {
if(name === 'get') {
return cup.reduce((val, fn)=> fn(val), value);
}
cup.push(target[name]);
return tmp;
}
});
return tmp;
};
})();
const target = {
add: _ => _ + 1,
sub: _ => _ - 1,
pow: _ => _ * 10,
division: _ => _ / 10
};
pipe(5, target).add.get;
6
No categories