美文网首页
javascript bind 部分应用

javascript bind 部分应用

作者: norberthu | 来源:发表于2019-09-29 16:56 被阅读0次
'use strict';

function add( a,b) {
    return a*10 +b;
}

function addPartical(fn, me) {
    return fn.bind(undefined,me);
}

const add10 = addPartical(add, 10);
console.log(add10(1));
// 101

const add5 = addPartical(add, 5);
console.log(add5(1));
//51

以前使用的时候,bind更多地是bind this。上面的例子属于函数式编程中的部分应用。巧妙的地方是一个函数的参数多次输入,可以导致很多参数更少,行为更具体的小函数(如add10, add5)。

相关文章

网友评论

      本文标题:javascript bind 部分应用

      本文链接:https://www.haomeiwen.com/subject/iytqpctx.html