Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.arity() Method

Underscore.js _.arity() Method

The Underscore.js _.arity() method returns a new function which is equivalent to the given function, except that the new function’s length property is equal to the number of Arguments. This does not limit the function to using that number of arguments. Its only effect is on the reported length.

Syntax:

_.arity( numberOfArgs, fun )

Parameters: This method takes two parameters as listed above and discussed below:

  • numberOfArgs: This parameter takes a number stating the number of arguments the method will take.
  • fun: This is the given function which is passed as parameter.

Return Value: It returns a new function.

Note: To execute the below examples, you have to install the underscore-contrib library by using this command prompt and execute the following command.

npm install underscore-contrib

Example 1: 




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun() {
    return "GFG";
}
  
var gfgFunc = _.arity(3, fun);
  
console.log("Length of function is :", gfgFunc.length);
console.log("Function content :", gfgFunc());


Output:

Length of function is : 3
Function content : GFG

Example 2:




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun() {
    return arguments[0]*10;
}
  
var gfgFunc = _.arity(3, fun);
  
console.log("Length of function is :", gfgFunc.length);
console.log("Function content :", gfgFunc(10));


Output: 

Length of function is : 3
Function content : 100
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments