Underscore.JS - uniqueId 方法

语法

_.uniqueId([prefix])

uniqueId 方法生成一个唯一的 ID,顾名思义。如果传递了前缀,则前缀将附加到该 ID。请参阅以下示例:

示例

var _ = require('underscore');

// 示例 1
var uniqueId1 = _.uniqueId("text");
// 示例 2
var uniqueId2 = _.uniqueId("text");

console.log(uniqueId1);
console.log(uniqueId2);

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

命令

\>node tester.js

输出

1
4
9
16
25

underscorejs_utilities.html