/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID():String{
}
先看看下面的几个方法
1.生成[0,1)的随机数的Math.random,例如
//我这次运行生成的是:0.5834165740043102
Math.random()
2.获取当前时间戳Date.now
//现在时间戳是1482645606622
Date.now()
3.将10进制转换为其他进制的字符串 Number.toString
//将1482645606622转换成二进制:10101100100110100100100001001000011011110
(1482645606622).toString(2)
//转换成16进制:159349090de MongDB中的ObjectID就是24位16进制数
(1482645606622).toString(16);
//最大进制支持转为36进制,使用字符是0-9a-z :ix48wvry
(1482645606622).toString(36)
GenNonDuplicateID的自我进化
1.随机数版本 v0.0.1
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(){
return Math.random().toString()
}
//生成一个类似 0.1283460319177394的ID
GenNonDuplicateID()
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(){
return Math.random().toString(16)
}
//函数将生成类似 0.c1615913fa915 的ID
GenNonDuplicateID()
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(){
return Math.random().toString(36)
}
//函数将生成类似 0.hefy7uw6ddzwidkwcmxkzkt9 的ID
GenNonDuplicateID()
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(){
return Math.random().toString(36).substr(3)
}
//函数将生成类似 8dlv9vabygks2cbg1spds4i 的ID
GenNonDuplicateID()
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(){
let idStr = Date.now().toString(36)
idStr += Math.random().toString(36).substr(3)
return idStr
}
//函数将生成类似 ix49sfsnt7514k5wpflyb5l2vtok9y66r 的ID
GenNonDuplicateID()
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(randomLength){
let idStr = Date.now().toString(36)
idStr += Math.random().toString(36).substr(3,randomLength)
return idStr
}
// GenNonDuplicateID(3) 将生成类似 ix49wl2978w 的ID
GenNonDuplicateID(3)
/**
* 生成一个用不重复的ID
*/
function GenNonDuplicateID(randomLength){
return Number(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}
//GenNonDuplicateID()将生成 rfmipbs8ag0kgkcogc 类似的ID
GenNonDuplicateID()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有