Javascript: binding to the right of a function?

心不动则不痛 提交于 2020-01-13 10:15:31

问题


How can I bind to the right of the function? Example:

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

回答1:


You're looking for partial functions, which are convenient shorthands for aliases.

The "classic" way to do what you're asking for is with:

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

Using partial functions it would be:

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

Unfortunately, Function.prototype.partial isn't provided in any browser.


Fortunately for you, I've been working on a library of what I consider to be essential JavaScript object oriented functions, methods, classes, etc. This is 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);
            };
        };
    }
}());



回答2:


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



回答3:


Lodash's partialRight will do what you want, and here is the documentation:

This method is like _.partial except that partial arguments
are appended to those provided to the new function.

Arguments
      func   (Function): The function to partially apply arguments to.
      [arg]  (…*)      : Arguments to be partially applied.
Returns      (Function): Returns the new partially applied function.



回答4:


This seems like you want partial application. There are a number of libraries that provide that, including underscore.js: http://documentcloud.github.com/underscore/




回答5:


You can do it with partial from underscore.js, by passing _ as a placeholder, to be filled-in later:

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

This feature appeared in February 2014 (underscore 1.6.0).




回答6:


What's wrong with:

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

To answer the question properly, you need to create an anonymous function that calls the "bound" function with a set parameter, such as:

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

In this manner, you can bind any number of parameters, rearrange parameters, or a combination of the two. However bear in mind that there is some impact to performance, as you are adding an extra function call to the stack every time you bind like this.



来源:https://stackoverflow.com/questions/9795106/javascript-binding-to-the-right-of-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!