Lodash - once method



Syntax

_.once(func)

Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation. The func is invoked with the this binding and arguments of the created function.

Arguments

  • func (Function) − The function to restrict.

Output

  • (Function) − Returns the new restricted function.

Example

var _ = require('lodash');
var create = function(){ console.log('Object Created.')};
var init = _.once(create);

init();
init();
init();
init();

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

Command

\>node tester.js

Output

Object Created.
lodash_function.htm
Advertisements