This could be rewritten with a simple if/else-statement. Var (variableName) = (condition) ? (valueIfConditionIsTrue) : (valueIfConditionIsFalse) #Uuid generator javascript code#Here a ternary operator is used to keep the required code short for the assignment short.Variable Definition 2 ( var v = c = 'x' ? r : (r & 0x3 | 0x8) ): So using bitwise operations will convert aįloat to an integer. ( )Īll bitwise operations except unsigned right shift, >, work on Without too much performance overhead we can replace this action with a dedicated floor function to improve readability. However, since bitwise operations work on 32-bit integers the result is cast and hence floored to an integer. Without going into too much detail of logic operations, a bitwise or with zero means that no bits are changed. By using the bitwise OR-operator with zero ( | 0), we basically floor the result, since the number ist cast to an integer. By multipling the value with 16, the result is between 0 and 16.Math.random() is a function that returns a value between 0 and 1.Variable Definition 1: ( r = Math.random() * 16 | 0 ): This line defines 2 variables, namely r and v. However, let's focus especially on your requested line and let me try to explain it step by step. For an updated solution of your function check out: and for more details on UUIDS: Additonally it does not stick to the RFC standards for UUIDS. However, its readability is very bad, in exchange for very short code. It is true that your method generates a more or less random string which could be used as a uuid.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |