谁能帮我解释一下js的这种写法是啥意思?UUID.prototype.createUUID = function()
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谁能帮我解释一下js的这种写法是啥意思?UUID.prototype.createUUID = function()相关的知识,希望对你有一定的参考价值。
利用JS生成32位的UUID(源码)
创建 UUID.js 文件, 然后复制以下内容:
// On creation of a UUID object, set it's initial value
function UUID()
this.id = this.createUUID();
// When asked what this Object is, lie and return it's value
UUID.prototype.valueOf = function() return this.id; ;
UUID.prototype.toString = function() return this.id; ;
//
// INSTANCE SPECIFIC METHODS
//
UUID.prototype.createUUID = function()
//
// Loose interpretation of the specification DCE 1.1: Remote Procedure Call
// since javascript doesn't allow access to internal systems, the last 48 bits
// of the node section is made up using a series of random numbers (6 octets long).
//
var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
var dc = new Date();
var t = dc.getTime() - dg.getTime();
var tl = UUID.getIntegerBits(t,0,31);
var tm = UUID.getIntegerBits(t,32,47);
var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);
// since detection of anything about the machine/browser is far to buggy,
// include some more random numbers here
// if NIC or an IP can be obtained reliably, that should be put in
// here instead.
var n = UUID.getIntegerBits(UUID.rand(8191),0,7) +
UUID.getIntegerBits(UUID.rand(8191),8,15) +
UUID.getIntegerBits(UUID.rand(8191),0,7) +
UUID.getIntegerBits(UUID.rand(8191),8,15) +
UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
return tl + tm + thv + csar + csl + n;
;
//Pull out only certain bits from a very large integer, used to get the time
//code information for the first part of a UUID. Will return zero's if there
//aren't enough bits to shift where it needs to.
UUID.getIntegerBits = function(val,start,end)
var base16 = UUID.returnBase(val,16);
var quadArray = new Array();
var quadString = '';
var i = 0;
for(i=0;i<base16.length;i++)
quadArray.push(base16.substring(i,i+1));
for(i=Math.floor(start/4);i<=Math.floor(end/4);i++)
if(!quadArray[i] || quadArray[i] == '') quadString += '0';
else quadString += quadArray[i];
return quadString;
;
//Replaced from the original function to leverage the built in methods in
//JavaScript. Thanks to Robert Kieffer for pointing this one out
UUID.returnBase = function(number, base)
return (number).toString(base).toUpperCase();
;
//pick a random number within a range of numbers
//int b rand(int a); where 0 <= b <= a
UUID.rand = function(max)
return Math.floor(Math.random() * (max + 1));
;
注: 使用时, 调用UUID.prototype.createUUID ()方法,返回32位UUID
然后给UUID原型添加方法
你也看到了UUID是自己定义的一个方法、在这里说就是一个对象吧 参考技术B 你可以理解为,给类添加一个方法。追问
UUID是一个类?prototype是一个固定写法?createUUID是一个方法?调用的时候用UUID.prototype.createUUID ()?UUID.prototype.createUUID ()为啥不写成function createUUID()方法,这种写法有什么好处?不太明白,您能帮我解释一下吗?
参考技术C javascript的神奇之处就是函数也是对象,prototype是对象原型,是想对象添加属性方法的接口本回答被提问者采纳以上是关于谁能帮我解释一下js的这种写法是啥意思?UUID.prototype.createUUID = function()的主要内容,如果未能解决你的问题,请参考以下文章
谁能帮我解释一下Dreamweaver中的CSS的display属性的none、block、inline等都用在啥场合?
我想知道avi、mp4、flv这几种视频格式的区别,谁能帮我解释一下