推荐两个库 shortid、hashmap,唯一标识生成和HashMap
发布于 10 年前 作者 shanelau 8695 次浏览 最后一次编辑是 8 年前

一、ShortId https://github.com/dylang/shortid

ShortId 能创建惊奇的不重复的url友好的唯一标识。

使用方法 var shortId = require(‘shortid’); console.log(shortId.generate());

二、HashMap 原生javascript是没有map数据结构的,借助一个库,实现了map所有功能

https://github.com/flesler/hashmap

3 回复

不错,两个库都很小巧。

自己集成到Underscore里,这样就更方便了。

我们一直用的 object 本身不就是 类hashmap的结构么? 为何还需要再实现一套hashmap呢? 突然想到之前有人 用node 自己实现 singleton… -_-
觉得应该多利用 js 和 node 语言本身给我们的优势 ~

js object 不支持 key 为非 string 类型。

同时,很容易写出这种错误的代码。

var map = {};
if (map.constructor) {
  console.log('it should not in here, but it just happened');
}

当然,几乎99%的情况下,node object 就已经够用了,不过还是有一些东西需要注意的。

回到顶部