【问题标题】:Javascript: binding to the right of a function?Javascript:绑定到函数的右侧?
【发布时间】:2012-04-05 09:50:48
【问题描述】:

如何绑定到函数的右侧?示例:

var square = Math.pow.bindRight(2);
console.log(square(3)); //desired output: 9

【问题讨论】:

    标签: javascript functional-programming


    【解决方案1】:
    Function.prototype.bindRight = function() {
        var self = this, args = [].slice.call( arguments );
        return function() {
            return self.apply( this, [].slice.call( arguments ).concat( args ) );
        };
    };
    
    var square = Math.pow.bindRight(2);
    square(3); //9
    

    【讨论】:

    • 我实际上期待使用标准库或流行库的实现。但是,似乎没有,所以这是正确的答案。谢谢。
    【解决方案2】:

    您正在寻找偏函数,它是别名的方便简写。

    执行您要求的“经典”方式是:

    var square = function (x) {
      return Math.pow(x, 2);
    };
    

    使用部分函数会是:

    var square = Math.pow.partial(undefined, 2);
    console.log(square(3));
    

    很遗憾,Function.prototype.partial 未在任何浏览器中提供。


    幸运的是,我一直在开发一个我认为是基本 JavaScript 面向对象的函数、方法、类等的库。这是Function.prototype.partial.js

    /**
     * @dependencies
     * Array.prototype.slice
     * Function.prototype.call
     * 
     * @return Function
     * returns the curried function with the provided arguments pre-populated
     */
    (function () {
        "use strict";
        if (!Function.prototype.partial) {
            Function.prototype.partial = function () {
                var fn,
                    argmts;
                fn = this;
                argmts = arguments;
                return function () {
                    var arg,
                        i,
                        args;
                    args = Array.prototype.slice.call(argmts);
                    for (i = arg = 0; i < args.length && arg < arguments.length; i++) {
                        if (typeof args[i] === 'undefined') {
                            args[i] = arguments[arg++];
                        }
                    }
                    return fn.apply(this, args);
                };
            };
        }
    }());
    

    【讨论】:

    • 我以前没有见过这个答案。这实际上是最好的解决方案。谢谢。
    • 另请参阅下面@brian-m-hunt 答案中的 lodash partialRight() 解决方案
    【解决方案3】:

    Lodash 的 partialRight 会做你想做的事,这里是文档:

    此方法类似于 _.partial,除了部分参数 附加到提供给新功能的那些。 论据 func (Function):部分应用参数的函数。 [arg] (…*) : 部分应用的参数。 Returns (Function):返回新的部分应用函数。

    【讨论】:

      【解决方案4】:

      这似乎你想要部分应用。有许多库提供了该功能,包括 underscore.js:http://documentcloud.github.com/underscore/

      【讨论】:

        【解决方案5】:

        您可以使用partial from underscore.js 来完成,通过传递_ 作为占位符,稍后填写:

        var square = _.partial(Math.pow, _, 2);
        console.log(square(3)); // => 9
        

        此功能于 2014 年 2 月出现(下划线 1.6.0)。

        【讨论】:

          【解决方案6】:

          有什么问题:

          var square = function(x) {return x*x;};
          

          要正确回答问题,需要创建一个匿名函数,调用带有设置参数的“绑定”函数,如:

          var square = function(x) {return Math.pow(x,2);};
          

          通过这种方式,您可以绑定任意数量的参数、重新排列参数或两者的组合。但是请记住,这会对性能产生一些影响,因为每次像这样绑定时都会向堆栈添加额外的函数调用。

          【讨论】:

          • 你的意思是除了他要求别的东西吗?
          • 我将不得不假设这是一个不好的例子,因为这个答案非常清楚地解决了给出的例子中的问题。
          • 给出的例子不是要解决的情况,它只是一个例子,所以你可以理解我需要什么。很抱歉,虽然意图是好的,但这不会这样做。
          • 回答正确。希望现在好多了。
          • 我认为 Dokkat 要求的是 bindRight 的通用实现。为特定功能编写它非常简单。
          猜你喜欢
          • 1970-01-01
          • 2013-03-13
          • 2010-11-26
          • 1970-01-01
          • 2010-12-18
          • 1970-01-01
          • 2011-10-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多