Function.bind.bind(Function.call)如何解除? [英] How does Function.bind.bind(Function.call) uncurry?
问题描述
var uncurryThis = Function.bind.bind(Function.call);
我正在努力解决这个问题。据推测,它不会发生。我如何解决这个问题?
我猜这是这个
被绑定到 Function.call
。没有足够的帮助。我还没有发现任何用途,所以我甚至不确定你是否独立调用它,或者需要将它称为作为方法,只有你知道,它首先绑定它。
<它将
调用
函数传递给 bind
函数,绑定
函数本身就是 this
的值。因此,你得到一个包含绑定
函数的包装,它将这个
设置为当你调用它时调用
函数。 反过来,它是一个函数,它允许您创建一个围绕调用
函数的包装器,该函数绑定到您传递它的某个参数。 如果您从今天早上醒来以来一直没有直接喝咖啡,请一步一步:
Function.bind.bind
是对绑定
函数的引用。该引用是从&mdash的一个属性生成的。困惑点1— 绑定
函数本身。请记住,绑定
函数在使用某个函数作为对象进行调用时,用于使用 this $ c $创建该函数的包装器c>绑定到传入的第一个参数。
Function.call.bind(something)
。
randomFunction.call(whatever)
。
因此:
函数random (){
alert(this.foo);
}
var bb = Function.bind.bind(Function.call);
var randomcall = bb(random);
randomcall({foo:hello world}); //警告hello world
终极点是这样的:你有一个函数,在函数内部有一些代码需要这个
具有一些属性,并且以这种或那种方式使用 this
。你真的希望能够在这里使用某个对象的功能,在那里有一些对象。你可以很明显的做到这一点。
random.call(someObject);
但是这个神奇的绑定绑定调用技巧为您提供了一种便宜的方法来创建变体你的函数可以避免显式编码调用 .call()
。它还可以让您将资深前端开发人员的职位更长一些。
edit —我打算破坏上面的妙语,因为我只是想到了使用bind + call技巧来获取函数的一个很好的理由,该函数可以调用一些期望通过来操作的函数。这个
在一些所有者对象上。假设您有一串字符串,并且您希望以小写字母获得这些字符串的版本。你可以这样写:
var uc = [Hello,World];
var lc = uc.map(function(s){return s.toLowerCase();});
但是通过神奇的bb函数,我们也可以写出:
var uc = [Hello,World];
var tlc = bb(String.prototype.toLowerCase);
var lc = uc.map(tlc);
用这种方式写的改进并不多,但是如果有人要做一组 bb()
所有方便的String原型方法的封装,这可能会更有意义。当然,一切都有价格,可能这种包装会对性能产生一定的影响。 (如果这样的做法很常见,那么运行时可能会改进。)
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?
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.
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
is a reference to thebind
function. The reference is generated from a property of — confusion point 1 — thebind
function itself. Remember, thebind
function, when called with some function as the object, is used to create a wrapper around that function withthis
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)
.
So:
function random() {
alert(this.foo);
}
var bb = Function.bind.bind(Function.call);
var randomcall = bb(random);
randomcall({ foo: "hello world" }); // alerts "hello world"
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);
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.
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(); });
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);
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屋!