javascript - 使用bind进行部分应用而不影响接收者

标签 javascript partial-application function-binding

如果我想部分应用一个函数,我可以使用bind,但似乎我必须影响函数的接收者(bind 的第一个参数)。这是正确的吗?

我想使用bind执行部分应用而不影响接收者。

myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver

最佳答案

partial application using bind without affecting the receiver

这是不可能的。 bind 被明确设计为部分应用“第零个参数” - this 值,以及可选的更多参数。如果您只想修复函数的第一个(可能还有更多)参数,但不绑定(bind) this,则需要使用不同的函数:

Function.prototype.partial = function() {
    if (arguments.length == 0)
        return this;
    var fn = this,
        args = Array.prototype.slice.call(arguments);
    return function() {
        return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
    };
};

当然,很多库也提供这样的功能,例如Underscore , Lodash , Ramda等等。但是没有本地等效项。

关于javascript - 使用bind进行部分应用而不影响接收者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744753/

相关文章:

haskell "Non type-variable argument in the constraint"

f# - 如何以紧凑的方式避免意外的部分应用?

javascript - 将 "this"绑定(bind)到 TypeScript Lambda

javascript - 创建日期对象

javascript - 在 JavaScript 中将表转换为对象数组

haskell - 在 Haskell 中,当你只传递一个列表时,map 函数是什么意思?

javascript - 在加载的ajax内容上绑定(bind)点击事件

javascript - XMLHttpRequest 和绑定(bind)回调不起作用

javascript - 用于获取具有特定属性和类的所有标签的选择器

javascript - 从元素列表中找出哪个元素被点击