每周有收获之--proxy 和管道函数

// 试用proxy 实现 管道函数 x |> fn1 |> fn2 |fn3

 

// 函数式编程之-函数的组合

// const add = (x)=>{

// return x+1;

// }

// const double = (x)=>{

// return x * 2;

// }

// const add100 = (x)=>{

// return x +1000;

// }

 

// const pipe = (...args)=>{

// return (x)=>{

// return args.reduce((pre,cur,ind,arr)=>{

// return cur(pre);

// },x)

// }

// }

// const pipeFn = pipe(add,double,add100);

// const result = pipeFn(2);

// console.log("result--->",result);

 

// 函数式编程之 函数的柯里化

// const add = (x)=>{

// return (y)=>{

// return x+y;

// }

// }

var checkage = min => (age => age > min);

var checkage18 = checkage(18); // 有点便函数的意思了,提高函数的试用性,减少函数的 普遍性

const isOver18 = checkage18(20);

console.log("isOver18-->", isOver18)

 

// proxy 实现管道函数 --- 也是利用reduce 实现代理

// const pipe = (value) => {

// const stack = [];

// const proxy = new Proxy({}, {

// get(target, prop) {

// if (prop === 'execute') {

// return stack.reduce(function (val, fn) {

// return fn(val);

// }, value);

// }

// stack.push(objFn[prop]);

// return proxy;

// }

// })

// return proxy;

// }

// var double = n => n * 2;

// var pow = n => n * n;

// const objFn = {

// double,

// pow,

// }

// pipe(3).double.pow.execute;

// 利用 proxy 实现 arr[-1] 等于 arr 数组的最后一项

const arr = [1,2,3,4,5,6,7,8];

const proxyArr = new Proxy(arr,{

get(target,propKey,receiver){

const length = target.length;

propKey = Number(propKey);

if(propKey< 0 ){

propKey = target.length + propKey

}

return target[propKey];

}

})

const re = proxyArr[-2];

console.log(`-1 ---->${re}`)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值