Implementation example of native JavaScript generation GUID _javascript tips

Source: Internet
Author: User
Tags repetition

A GUID (global uniform identifier) is a number that is generated on a single machine and is guaranteed to be unique to all machines in the same space-time. Typically, the platform provides an API to generate GUIDs. The generation algorithm is interesting, using the Ethernet card address, nanosecond time, chip ID code, and many possible numbers. The only drawback to GUIDs is that the resulting string is larger.

The GUID is in the form of: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

We all know that GUIDs are not very useful in front-end development, but if you need to insert an ID, and this ID corresponds to the backend and other operations that require GUIDs, we can generate a GUID for convenience.

Generally, in SQL, Java, C #, and other background or database language to generate a GUID is very simple, and the front-end does not directly generate a GUID method, you can only hand-write one. However, because the GUID needs to obtain the address of the Ethernet card, as well as the nanosecond time number. And the front-end access to this information is more difficult (know the child shoes please be sure to tell me), and we can simulate the implementation of the build GUID, the code is as follows:

/*
* Function: Generate a GUID code, the GUID is composed of 14 below date time and more than 18 16 random numbers, the GUID has a certain repetition probability, but the repetition probability is very low, in theory the repetition probability is 1/(16^18) per 10ms, i.e. 16 18 times 1. The repetition probability is too low to be negligible * *

function GUID () {this.date = new date (); /* To determine whether the initialization, if the following code is initialized, the following code will no longer execute, in practice only once/if (typeof this.newguid!= ' function ') {/* Generate GUID Code * * * * GUID.prototype.newGU ID = function () {this.date = new date (); var guidstr = '; sexadecimaldate = This.hexadecimal (This.getguiddate (),); SE
Xadecimaltime = This.hexadecimal (This.getguidtime (), 16);
for (var i = 0; i < 9; i++) {guidstr + = Math.floor (Math.random () *16). toString (); Guidstr + = Sexadecimaldate;
Guidstr + = Sexadecimaltime; while (Guidstr.length <) {Guidstr + = Math.floor (Math.random () *16). ToString (); return This.formatguid (GUIDSTR)
; /* * Feature: Gets the GUID format of the current date, that is, 8-digit Date: 19700101 * Return value: Returns the GUID of the date format of the message/GUID.prototype.getGUIDDate = function () {return This.da
Te.getfullyear () + This.addzero (this.date.getMonth () + 1) + This.addzero (This.date.getDay ()); /* * Feature: Gets the GUID format of the current time, that is, 8-digit time, including milliseconds, milliseconds is 2 digits: 12300933 * return value: Return GUID/GUID.prototype.getGUIDTime = function () {RE Turn This.addzero (this.date.getHours ()) + This.addZero (This.date.getMinutes ()) + This.addzero (This.date.getSeconds ()) + This.addzero (parseint (
This.date.getMilliseconds () ()/10)); /* * Feature: Add 0 to front of a positive integer for one number, if it is a string that can be converted to a non-Nan number, it can also implement the * parameter: the parameter represents a string that is ready to be added to the previous 0 or that can be converted to a number * return value: Returns the string type after adding 0 if it is eligible, otherwise returns its own strings * /GUID.prototype.addZero = function (num) {if (number (num). toString ()!= ' NaN ' && num >= 0 && Num <

{return ' 0 ' + math.floor (num);} else {return num.tostring ();}}
* * Function: The value of the Y-system, numeric * parameters converted to x: The 1th parameter represents the value to convert, and the 2nd parameter represents the binary to convert, and the 3rd argument is optional, representing the current number, if not 10 * return value: Return the converted string.  GUID.prototype.hexadecimal = function (num, x, y) {if (y!= undefined) {return parseint (num.tostring (), y). toString (x);

else {return parseint (num.tostring ()). toString (x);}} * * Function: Format 32-bit string is a GUID mode string * parameter: The 1th parameter represents a 32-bit string * return value: Standard GUID format string/GUID.prototype.formatGUID = function (guidstr) {VA R str1 = Guidstr.slice (0, 8) + '-', str2 = Guidstr.slice (8,) + '-', STR3 = Guidstr.slice (a) + '-', STR4 = Guidstr. Slice + '-', STR5 = Guidstr.slice (20);
Return str1 + str2 + str3 + STR4 + str5; }
}
}

GUID Object

Just save it in a JS file and refer to it.

And then we just need

var guid = new GUID ();

Alert (Guid.NewGuid ());

The GUID code can be obtained.

The implementation principle is very simple, here only uses the system time and more than 18 hexadecimal random number composition, and uses the system time conversion to 16, thus although still may duplicate, but the repetition probability is extremely low, negligible.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.