js随机生成不重复的key
2021-04-09 本文已影响0人
隔壁老王z
function closure() {
const cache = {}
return function getRandom() {
let m = Math.random()
while (cache[m]) {
m = Math.random()
}
cache[m] = m
return m
}
}
const generateKey = closure()
// 生成
console.log(generateKey())
console.log(generateKey())
console.log(generateKey())
console.log(generateKey())