The _.mapArgs() method takes a target function and returns a new function that accepts a mapping function, which in turn returns a function that will map its arguments before calling the original target function.
Syntax:
_.mapArgs( target_function, mapping_function );
Parameters:
- target_function: Original called function.
- mapping_function: Mapping function to be accepted by the function.
Return Value: This method returns a function.
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed. Underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1: We made function that cubes the given value then adds that value to itself.
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
function add (x) {
return x + x;
} function cube (x) {
return x * x * x;
} var cubethenadd = _.mapArgs(add)(cube);
console.log(cubethenadd(5)) |
Output:
250
Example 2:
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );
function add (x) {
return x + x; }
function sub (x) {
return x - x;
} var subthenadd = _.mapArgs(add)(sub);
console.log(subthenadd(5)) |
Output:
0