JS 生成GUID 方法

var Guid={NewGuid: function () {
                var guid = (this._G() + this._G() +"-"+ this._G() +"-"+ this._G() +"-"+ this._G() +"-"+ this._G() + this._G() + this._G()).toUpperCase();
                return guid;
            },
            _G: function () {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }
 };
console.log(Guid.NewGuid());
console.log(Guid.NewGuid());
console.log(Guid.NewGuid());
console.log(Guid.NewGuid());
// rfc4122 version 4
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
});

 

posted @ 2016-05-09 16:55  孙首富  阅读(1876)  评论(0)    收藏  举报