绑定与咖喱的Javascript用法? [英] Javascript usages of bind vs curry?

查看:61
本文介绍了绑定与咖喱的Javascript用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解咖喱绑定之间的区别。

I'm trying to understand the difference between curry vs bind.

bind 的实现是:

/*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*/   }

执行咖喱是:

/*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*/   };

我已经知道咖喱不是内部函数(与IE9 +中的 bind 不同)。但仍然:

I already know that curry is not an internal function (unlike bind which is in IE9+). But still:

为什么我听到人们一直在谈论咖喱,虽然他们可以简单地使用绑定操作?

Why do I hear people keep talking about curry , While they can simply use bind operation ?

唯一的区别是实际上只在 bind 函数中找到的上下文。

The only difference is the context which is actually found only at the bind function.

差异

示例:

假设我有这个功能:

function add(x,y,z)
{
  return x+y+z;
}

我可以用咖喱

alert(add.curry(2).curry(1)(4)) //7

但我也可以这样做:

alert(add.bind(undefined,2).bind(undefined,1)(4)) //7

我不明白为什么这个 curry term函数存在,同时可以向bind函数添加一个伪上下文。

I don't understand why this curry term function exists while it is possible to add a dummy context to the bind function.

我错过了什么?

推荐答案

bind 强制您将上下文附加到函数中,同时通过使用 curry ,您可以延迟函数上下文的指定,直到调用 curried函数,在许多情况下都很有用。

考虑以下示例(不是完美的,只是为了说明这个想法):

bind forces you to attach a context to the function, while by using curry, you can delay the specification of function context until invoking the curried function, useful in many cases.
consider the following example (not the perfect one, just to illustrate the idea):

function Query(sessionKey, dataBuilder) {
  this.sessionKey = sessionKey;
  this.url = "http://www.example.com/search";
  this.dataBuilder = dataBuilder
  this.search = function (term) {
    $.ajax({
      type: "POST",
      url: this.url,
      data: this.dataBuilder(term);
    })
  }
}

function dataBuilder(entity, query) {
  var payload = JSON.stringify({
    'entity': entity,
    'searchTerm': query
    'session': this.sessionKey // will be always undefined if bind(undefined,...) is used
  });
  return payload
}
var bindEx= dataBuilder.bind(undefined, "username");
var curryEx= dataBuilder.curry("username");

var usernameQuery = new Query("id1234",bindEx); // won't work, this.sessionKey will be undefined
usernameQuery = new Query("id1234",curryEx); // will  work, this.sessionKey will be id1234 in the DataBuilder

这篇关于绑定与咖喱的Javascript用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆