Open In App

Underscore.js _.mapArgs() Method

Last Updated : 18 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Javascript




// 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:

Javascript




// 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads