var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
is the same as
var r = Math.random() * 16 | 0;
ie, create a random number in the range from 0-15 (or 0-f in hex) without decimal places. You could also write this line as
var r = Math.floor(Math.random() * 16)
but | 0
is probably faster ... And
var v = c == 'x' ? r : (r & 0x3 | 0x8);
ie, depending on the value of the current character to replace (ie 'x' or 'y') use either r
or r | 0x3 | 0x8
as value for the current place. The latter is because of specification of UUID version 4, that certain bits must have certain values. See specs for details.
You can rewrite this line as follows
var v = 0;
if (c == 'x') v = r;
else v = r & 0x3 | 0x8
So v
is still a value between 0 and 15, which is than converted to a hex char (0 - f) with v.toString(16)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…