Lodash - once 方法

语法

_.once(func)

创建一个限制为调用 func 一次的函数。重复调用该函数将返回第一次调用的值。使用创建的函数的 this 绑定和参数调用 func。

参数

  • func (Function) − 要限制的函数。

输出

  • (Function) − 返回新的受限函数。

示例

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

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

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

Object Created.

lodash_function.html