An example implementation of native JavaScript generating guids
- 2020-03-30 03:54:23
- OfStack
A GUID (global uniform identifier) is a number generated on a single machine that is guaranteed to be unique to all machines in the same space and time. Usually the platform provides an API for generating guids. The algorithm is interesting, using Ethernet card addresses, nanoseconds, chip ID codes, and many possible Numbers. The only drawback with GUID is that the resulting strings are large.
The format of GUID is: XXXXXXXX - XXXX - XXXX - XXXX - XXXXXXXXXXXX
You all know that GUID is not very useful in front-end development, but if you need to insert an ID, and this ID corresponds to the background and other operations that require GUID, you can still generate a GUID for convenience.
In general, it is very simple to generate GUID in SQL, Java, C# and other background or database languages, and the front end does not directly generate GUID method, can only write one by hand. But because GUID needs to get the Ethernet card's address, nanosecond time, and so on. However, it is difficult for the front end to obtain these information (please tell me if you know children's shoes), and we can simulate the realization of generating GUID, the code is as follows:
/ *
* function: generate a GUID code, in which GUID is composed of less than 14 dates and time and more than 18 hexadecimal random Numbers. GUID has a certain probability of repetition, but the probability of repetition is very low. Theoretically, the probability of repetition is 1/(16^18) per 10ms, that is, 1/ 16 to the 18th power
function GUID() {
this.date = new Date();
if (typeof this.newGUID != 'function') {
GUID.prototype.newGUID = function() {
this.date = new Date();
var guidStr = '';
sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
for (var i = 0; i < 9; i++) {
guidStr += Math.floor(Math.random()*16).toString(16);
}
guidStr += sexadecimalDate;
guidStr += sexadecimalTime;
while(guidStr.length < 32) {
guidStr += Math.floor(Math.random()*16).toString(16);
}
return this.formatGUID(guidStr);
}
GUID.prototype.getGUIDDate = function() {
return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
}
GUID.prototype.getGUIDTime = function() {
return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero( parseInt(this.date.getMilliseconds() / 10 ));
}
GUID.prototype.addZero = function(num) {
if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
return '0' + Math.floor(num);
} else {
return num.toString();
}
}
GUID.prototype.hexadecimal = function(num, x, y) {
if (y != undefined) {
return parseInt(num.toString(), y).toString(x);
} else {
return parseInt(num.toString()).toString(x);
}
}
GUID.prototype.formatGUID = function(guidStr) {
var str1 = guidStr.slice(0, 8) + '-',
str2 = guidStr.slice(8, 12) + '-',
str3 = guidStr.slice(12, 16) + '-',
str4 = guidStr.slice(16, 20) + '-',
str5 = guidStr.slice(20);
return str1 + str2 + str3 + str4 + str5;
}
}
}
GUID object
All you need to do is save it in a JS file and reference it.
And then we just have to
Var guid = new guid ();
Alert (guid. NewGUID ());
You can get the GUID code.
The implementation principle is very simple, here only used the system time and more than 18 hexadecimal random number composition, and the system time conversion to hexadecimal, although it is still possible to repeat, but the probability of repetition is very low, can be ignored.