The _.methodize() method takes a function and pulls the first argument out of the argument list and into this position. The returned function calls the original with its receiver (this) prepending the argument list.
Syntax:
_.methodize( function );
Parameters: This method accepts a single parameter as mentioned above and described below:
- function: original function containing arguments.
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:
Javascript
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );Â Â Â function gfgFunc (obj) { Â Â Â Â return obj.name + ": " + obj.about; } Â Â var Geeks = { Â Â Â Â name: "neveropen" , Â Â Â Â about: "Computer Science Portal for Geeks" , Â Â Â Â fun: _.methodize(gfgFunc) }; Â Â console.log(Geeks.fun()) |
Output:
neveropen: Computer Science Portal for Geeks
Example 2:Â
Javascript
// Defining underscore contrib variable var _ = require( 'underscore-contrib' );Â Â Â function gfgFunc (obj) { Â Â Â Â return "Geeks" ; } Â Â fun= _.methodize(gfgFunc) console.log(fun()) |
Output:
Geeks