Underscore.JS - once 方法

语法

_.once(function)

once 方法返回传递函数的副本,并确保结果函数无论被调用多少次都只被调用一次。请参阅以下示例 −

示例

var _ = require('underscore');

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

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

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

命令

\>node tester.js

输出

Object Created.

underscorejs_functions.html