Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JSHint argue against bitwise operators? How should I express this code?

I am using this bit of JavaScript to generate a UID:

(original:)

//If ID has not been defined then generate a new unique ID. if(!id){     id = '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); }); } 

(formatted so it can be read:)

// If ID has not been defined then generate a new unique ID. if (!id) {     id = '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);          }     ); } 

JSHint does not like the use of bitwise OR and AND operators. I was wondering how I could rewrite this to be more 'standard friendly.'

EDIT: JSHint states:

Line 8: id = '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); }); Unexpected use of '|'.  Line 8: id = '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); }); Expected '===' and instead saw '=='.  Line 8: id = '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); }); Unexpected use of '&'.  Line 8: id = '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); }); Unexpected use of '|'. 
like image 649
Sean Anderson Avatar asked Jul 23 '12 00:07

Sean Anderson


1 Answers

Put

/*jshint bitwise: false*/ 

in the top of your file

A list of available options: http://jshint.com/docs/options/

like image 99
zerkms Avatar answered Sep 28 '22 15:09

zerkms