Function.bind.bind(Function.call) 是如何解咖喱的? [英] How does Function.bind.bind(Function.call) uncurry?

查看:20
本文介绍了Function.bind.bind(Function.call) 是如何解咖喱的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的代码库中有这一行:

We have this line in my code base:

var uncurryThis = Function.bind.bind(Function.call);

我正在努力解决这个问题.据推测,它不加咖喱.我该如何解决这个问题?

That I'm trying to work through. Presumably, it uncurries. How do I work this out?

我猜这是Function.bind 的一个版本,它自己的this 绑定到Function.call.对我帮助不够.而且我还没有发现任何用途,所以我什至不确定你是将它独立调用还是需要将它称为作为方法",只是,你知道,先绑定它.

I guess it's a version of Function.bind whose own this is bound to Function.call. Doesn't help me enough. And I haven't found any uses, so I'm not even sure if you call it standalone or need to call it "as a method", only, you know, bind it first.

推荐答案

它将call函数传递给bind函数,用bind> 函数本身就是 this 的值.因此,您会得到一个围绕 bind 函数的包装器,当您调用它时,它会将 this 安排为 call 函数.那个反过来又是一个函数,它允许您围绕 call 函数创建一个包装器,该函数绑定到您传递给它的某个参数.

It passes the call function to the bind function, with the bind function itself being the value of this. Thus you get in return a wrapper around the bind function that arranges for this to be the call function when you call it. That, in turn, is a function that lets you create a wrapper around the call function bound to some argument you pass it.

如果你从今早起床后就没有不停地喝咖啡,那么一步一步来:

In case you haven't been drinking coffee nonstop since you woke up this morning, step by step:

  • Function.bind.bind 是对 bind 函数的引用.该引用是从 — 的属性生成的.混淆点1bind 函数本身.请记住,bind 函数在以某个函数作为对象调用时,用于围绕该函数创建一个包装器,其中 this 绑定到传入的第一个参数.莉>
  • 因此,该函数调用会返回一个函数.这个函数就像你调用了 Function.call.bind(something) 一样工作.
  • 如果你将一些随机函数作为参数传递给 那个 函数,那么,你会得到一个随机函数的包装器,当被调用时,它会像 randomFunction.call(whatever).
  • Function.bind.bind is a reference to the bind function. The reference is generated from a property of — confusion point 1 — the bind function itself. Remember, the bind function, when called with some function as the object, is used to create a wrapper around that function with this bound to the first argument passed in.
  • Thus that function call gives you a function back. That function works as if you called Function.call.bind(something).
  • If you pass some random function as an argument to that function, then, you get back a wrapper around the random function that, when called, will act like randomFunction.call(whatever).

所以:

function random() {
  alert(this.foo);
}

var bb = Function.bind.bind(Function.call);

var randomcall = bb(random);

randomcall({ foo: "hello world" }); // alerts "hello world"

最终点是这样的:你有一个函数,在函数内部的代码期望 this 有一些属性,它使用 this 合二为一方式或其他.您真的希望能够使用该函数处理这里的某个对象,那里的某个对象.你显然可以用

The ultimate point is this: you've got a function, and inside the function there's code that expects this to have some properties, and it uses this in one way or another. You'd really like to be able to use that function with some object here, some object there. You can obviously do that with

random.call(someObject);

但是这个神奇的绑定-绑定-调用"技巧为您提供了一种廉价的方法来创建您的函数的变体,让您避免对 .call() 的显式编码调用.它还可以让您在高级前端开发人员的职位上多留一点时间.

But this magic "bind-bind-call" trick gives you a cheap way to create a variation on your function that lets you avoid the explicitly-coded invocation of .call(). It also allows you to hang onto your senior front-end developer position for a little bit longer.

编辑 —我要破坏上面的妙语,因为我刚刚想到了一个很好的理由使用 bind+call 技巧来获得一个函数,该函数安排调用一些期望通过 this 在一些所有者"对象上.假设您有一个字符串数组,并且您想获得这些字符串的小写版本.你可以这样写:

edit — I'm going to spoil the punch line above because I just thought of a good reason to use the bind+call trick to obtain a function that arranges to make a call to some desired function that expects to operate via this on some "owner" object. Let's say you've got an array of strings, and you'd like to get a version of those strings in lower-case. You could write this:

var uc = ["Hello", "World"];
var lc = uc.map(function(s) { return s.toLowerCase(); });

但是使用神奇的bb"函数我们也可以这样写:

But with the magic "bb" function we could also write:

var uc = ["Hello", "World"];    
var tlc = bb(String.prototype.toLowerCase);
var lc = uc.map(tlc);

以这种方式编写的改进不大,但是如果要为所有方便的 String 原型方法制作一组 bb() 化的包装器,它可能更有意义.当然,一切都是有代价的,而且很可能这样的包装器会对性能产生一些影响.(如果这样的做法很普遍,那么运行时可能会得到改进.)

Not much of an improvement written that way, but if one were to make a set of bb()-ified wrappers of all the handy String prototype methods, it might make more sense. Of course, everything has a price, and it's probably the case that such wrappers will have some performance impact. (If practices like this were common then runtimes could probably be improved.)

这篇关于Function.bind.bind(Function.call) 是如何解咖喱的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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