Javascript 为什么 JSHint 反对按位运算符?我应该如何表达这段代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11605068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does JSHint argue against bitwise operators? How should I express this code?
提问by Sean Anderson
I am using this bit of JavaScript to generate a UID:
我正在使用这段 JavaScript 来生成一个 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.'
JSHint 不喜欢使用按位 OR 和 AND 运算符。我想知道如何将其重写为更“标准友好”。
EDIT: JSHint states:
编辑:JSHint 指出:
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 '|'.
回答by zerkms
Put
放
/*jshint bitwise: false*/
in the top of your file
在文件顶部
A list of available options: http://jshint.com/docs/options/
可用选项列表:http: //jshint.com/docs/options/
回答by eczajk
According to JSHint docs, it is because
根据JSHint 文档,这是因为
"Bitwise operators are very rare in JavaScript programs"
“按位运算符在 JavaScript 程序中非常少见”
As others have mentioned, you can disable the bitwise
JSHint option to squelch the warnings.
正如其他人提到的,您可以禁用bitwise
JSHint 选项来消除警告。
回答by Ned Batchelder
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:
您将如此多的代码塞进了一行(为什么??),以至于您无法分辨 jshint 向您指出了什么。我重新格式化了代码,我看到了这个:
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.
什么是| 0
做什么呢?这是一个不必要的空操作。更新:似乎是一种整合浮点数的方法。
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.
Jshint 似乎不喜欢其他东西,但至少摆脱了这一点。并展开您的代码,以便您(和其他人)可以阅读它。
回答by Jaime
About the "Why argue against bitwise operators". I like this from TSLint docs
关于“为什么要反对按位运算符”。我喜欢TSLint 文档中的这个
Bitwise operators are often typos (...) They also can be an indicator of overly clever code which decreases maintainability.
按位运算符通常是拼写错误 (...) 它们也可能表明代码过于聪明,从而降低了可维护性。
回答by Тимур Аппаев
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();