45

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 '|'.
4

5 に答える 5

70

Put

/*jshint bitwise: false*/

in the top of your file

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

于 2012-07-23T00:17:05.660 に答える
17

According to JSHint docs, it is because

"Bitwise operators are very rare in JavaScript programs"

As others have mentioned, you can disable the bitwise JSHint option to squelch the warnings.

于 2016-07-01T14:44:56.360 に答える
6

You've jammed so much code into one line (why??) that you can't tell what jshint is pointing out to you. I've reformatted the code, and I see this:

var r = Math.random() * 16 | 0, 

What is the | 0 doing there? It's a needless no-op. UPDATE: seems to be a way to int-ify a float.

Jshint seems to not like other things, but at least get rid of this. And spread your code out so that you (and others) can read it.

于 2012-07-23T00:19:41.520 に答える
2

About the "Why argue against bitwise operators". I like this from TSLint docs

Bitwise operators are often typos (...) They also can be an indicator of overly clever code which decreases maintainability.

于 2017-10-17T14:57:05.567 に答える
-1

this works fine for me:

return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
    function(c: string) {
        var r = Math.floor(Math.random() * 16), 
            v = c === 'x' ? r : (r % 4 + 4);
        return v.toString(16);          
    }).toUpperCase();
于 2016-04-21T10:58:08.177 に答える