Underscore.JS - partial method



Syntax

_.partial(function, *arguments)

partial method partially apply a function by giving its any arguments._ can be passed to specify an argument that should not be pre-filled, but left open to supply at call-time. See the below example

Example

var _ = require('underscore');

var divide = function(a, b) { return b / a};

var divideBy5 = _.partial(divide, 5);
var result = divideBy5(10);

console.log(result);

var divisionOf10 = _.partial(divide, _, 10)
result = divisionOf10(5)

console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

2
2
underscorejs_functions.htm
Advertisements