Javascript: binding to the right side of a function?

How can I snap the right part of a function? Example:

var square = Math.pow.bindRight(2); console.log(square(3)); //desired output: 9 
+5
source share
6 answers

You are looking for partial functions that are convenient abbreviations for aliases.

The "classic" way of doing what you ask for is:

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

Using partial functions, this will be:

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

Unfortunately, Function.prototype.partial not provided in any browser.


Luckily for you, I worked on a library of what I consider to be essential object-oriented JavaScript 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); }; }; } }()); 
+6
source
 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 
+11
source

Lodash 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.
+3
source

It seems that you need a partial application. There are several libraries that provide this, including underscore.js: http://documentcloud.github.com/underscore/

0
source

You can do this with partial from underscore.js , passing _ as a placeholder, which will be filled later

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

This feature appeared in February 2014 (underscore 1.6.0 ).

0
source

What happened with:

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

To correctly answer the question, you need to create an anonymous function that calls the "bound" function with the given parameter, for example:

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

Thus, you can associate any number of parameters, change the parameters or their combination. However, keep in mind that there is some kind of performance impact as you add an extra function call to the stack every time you bind this.

-1
source

Source: https://habr.com/ru/post/920640/


All Articles