你想隨機生成一個唯一的標(biāo)識符。
可以根據(jù)一個隨機數(shù)值生成一個 Base 36 編碼的字符串。
uniqueId = (length=8) ->
id = ""
id += Math.random().toString(36).substr(2) while id.length < length
id.substr 0, length
uniqueId() # => n5yjla3b
uniqueId(2) # => 0d
uniqueId(20) # => ox9eo7rt3ej0pb9kqlke
uniqueId(40) # => xu2vo4xjn4g0t3xr74zmndshrqlivn291d584alj
使用其他技術(shù)也可以,但這種方法相對來說性能更高,也更靈活。