我试图理解之间的区别curry
vs bind
.
实施bind
is :
/*1*/ Function.prototype.bind = function ()
/*2*/ {
/*3*/ var fn = this,
/*4*/ args = Array.prototype.slice.call(arguments);
/*5*/ var object = args.shift();
/*6*/ return function ()
/*7*/ {
/*8*/ return fn.apply(object,
/*9*/ args.concat(Array.prototype.slice.call(arguments)))
/*10*/ };
/*11*/ }
实施curry
is :
/*1*/ Function.prototype.curry = function ()
/*2*/ {
/*3*/ var fn = this,
/*4*/ args = Array.prototype.slice.call(arguments);
/*5*/ return function ()
/*6*/ {
/*7*/ return fn.apply(this,
/*8*/ args.concat(Array.prototype.slice.call(arguments)));
/*9*/ };
/*10*/ };
我已经知道了curry
不是内部函数(与bind
这是在 IE9+ 中)。但仍然:
为什么我听到人们一直在谈论curry
,虽然他们可以简单地使用bind
手术 ?
唯一的区别是上下文实际上只能在bind
功能。
差异 https://i.stack.imgur.com/SvK0d.jpg
例子 :
假设我有这个功能:
function add(x,y,z)
{
return x+y+z;
}
我可以用curry
:
alert(add.curry(2).curry(1)(4)) //7
但我也可以这样做:
alert(add.bind(undefined,2).bind(undefined,1)(4)) //7
我不明白为什么会这样curry
术语函数存在,同时可以向绑定函数添加虚拟上下文。
我缺少什么?