Javascript JSLint 报告意外使用了“&”和“|” -- 我想清理这个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3039748/
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
JSLint reports unexpected use of '&' and '|' -- I'd like to clean this
提问by Zhami
I'm trying to get my Javascript code 100% JSLint clean.
我试图让我的 Javascript 代码 100% JSLint 干净。
I've got some JS code that I've lifted from elsewhere to create a UUID. That code has the following line:
我从其他地方提取了一些 JS 代码来创建 UUID。该代码具有以下行:
s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);
This line incites JSLint to generate two error messages:
这一行促使 JSLint 生成两条错误消息:
1) Unexpected use of '&'
2) Unexpected use of '|'
I don't understand why -- I'd appreciate counsel regarding how to recode to eliminate the error message.
我不明白为什么 - 我很感激有关如何重新编码以消除错误消息的建议。
回答by Ben Zotto
The reason "why" is that actual bitwise operations are exceedingly rare in JS, and those operators appearing in JS code almost always are a typo for the boolean versions (&&, ||). That's why JSLint cares. This is a legit use of bitwise ops though. I believe you can silence the warning with the bitwiseflag:
“为什么”的原因是实际的按位运算在 JS 中极其罕见,并且出现在 JS 代码中的那些运算符几乎总是布尔版本 ( &&, ||)的拼写错误。这就是 JSLint 关心的原因。不过,这是按位操作的合法使用。我相信您可以使用bitwise标志使警告静音:
/*jslint bitwise: true */
回答by Michael Mrozek
Did you give it the bitwiseoption? That option warns on all uses of bitwise operations, as they tend to be inefficient in Javascript (the native floats need to be converted to ints for the bitwise operation, and then converted back)
你给它bitwise选择了吗?该选项对按位运算的所有使用发出警告,因为它们在 Javascript 中往往效率低下(本地浮点数需要转换为整数以进行按位运算,然后再转换回来)
回答by broofa
If you're using this javascript linttool you can specify a configuration file on the command line that specifies which checks to enable/disable.
如果您使用这个 javascript lint工具,您可以在命令行上指定一个配置文件,指定启用/禁用哪些检查。
For example, I typically invoke mine as follows:
例如,我通常按如下方式调用我的:
jsl -conf jsl.conf
with a jsl.conf file that looks like this
使用如下所示的 jsl.conf 文件
#----------------------------------------------------------------------------
#
# This is a slightly edited version of the jsl.default.conf file that comes
# with the install package for JavaScript Lint.
#
#----------------------------------------------------------------------------
#
# Configuration File for JavaScript Lint 0.3.0
# Developed by Matthias Miller (http://www.JavaScriptLint.com)
#
# This configuration file can be used to lint a collection of scripts, or to
# enable or disable warnings for scripts that are linted via the command line.
#
### Warnings
# Enable or disable warnings based on requirements.
# Use "+WarningName" to display or "-WarningName" to suppress.
#
-no_return_value # function {0} does not always return a value
+duplicate_formal # duplicate formal argument {0}
-equal_as_assign # test for equality (==) mistyped as assignment (=)?{0}
+var_hides_arg # variable {0} hides argument
-redeclared_var # redeclaration of {0} {1}
-anon_no_return_value # anonymous function does not always return a value
+missing_semicolon # missing semicolon
+meaningless_block # meaningless block; curly braces have no impact
+comma_separated_stmts # multiple statements separated by commas (use semicolons?)
+unreachable_code # unreachable code
-missing_break # missing break statement
-missing_break_for_last_case # missing break statement for last case in switch
-comparison_type_conv # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==)
-inc_dec_within_stmt # increment (++) and decrement (--) operators used as part of greater statement
+useless_void # use of the void type may be unnecessary (void is always undefined)
+multiple_plus_minus # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs
+use_of_label # use of label
-block_without_braces # block statement without curly braces
-leading_decimal_point # leading decimal point may indicate a number or an object member
+trailing_decimal_point # trailing decimal point may indicate a number or an object member
-octal_number # leading zeros make an octal number
+nested_comment # nested comment
-misplaced_regex # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma
-ambiguous_newline # unexpected end of line; it is ambiguous whether these lines are part of the same statement
-empty_statement # empty statement or extra semicolon
-missing_option_explicit # the "option explicit" control comment is missing
+partial_option_explicit # the "option explicit" control comment, if used, must be in the first script tag
+dup_option_explicit # duplicate "option explicit" control comment
+useless_assign # useless assignment
-ambiguous_nested_stmt # block statements containing block statements should use curly braces to resolve ambiguity
+ambiguous_else_stmt # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent)
-missing_default_case # missing default case in switch statement
+duplicate_case_in_switch # duplicate case in switch statements
+default_not_at_end # the default case is not at the end of the switch statement
+legacy_cc_not_understood # couldn't understand control comment using /*@keyword@*/ syntax
+jsl_cc_not_understood # couldn't understand control comment using /*jsl:keyword*/ syntax
+useless_comparison # useless comparison; comparing identical expressions
+with_statement # with statement hides undeclared variables; use temporary variable instead
+trailing_comma_in_array # extra comma is not recommended in array initializers
+assign_to_function_call # assignment to a function call
-parseint_missing_radix # parseInt missing radix parameter
### Output format
# Customize the format of the error message.
# __FILE__ indicates current file path
# __FILENAME__ indicates current file name
# __LINE__ indicates current line
# __ERROR__ indicates error message
#
# Visual Studio syntax (default):
+output-format __FILE__(__LINE__): __ERROR__
# Alternative syntax:
#+output-format __FILE__:__LINE__: __ERROR__
### Context
# Show the in-line position of the error.
# Use "+context" to display or "-context" to suppress.
#
+context
### Semicolons
# By default, assignments of an anonymous function to a variable or
# property (such as a function prototype) must be followed by a semicolon.
#
+lambda_assign_requires_semicolon
### Control Comments
# Both JavaScript Lint and the JScript interpreter confuse each other with the syntax for
# the /*@keyword@*/ control comments and JScript conditional comments. (The latter is
# enabled in JScript with @cc_on@). The /*jsl:keyword*/ syntax is preferred for this reason,
# although legacy control comments are enabled by default for backward compatibility.
#
#+legacy_control_comments
### JScript Function Extensions
# JScript allows member functions to be defined like this:
# function MyObj() { /*constructor*/ }
# function MyObj.prototype.go() { /*member function*/ }
#
# It also allows events to be attached like this:
# function window::onload() { /*init page*/ }
#
# This is a Microsoft-only JavaScript extension. Enable this setting to allow them.
#
-jscript_function_extensions
### Defining identifiers
# By default, "option explicit" is enabled on a per-file basis.
# To enable this for all files, use "+always_use_option_explicit"
-always_use_option_explicit
# Define certain identifiers of which the lint is not aware.
# (Use this in conjunction with the "undeclared identifier" warning.)
#
# Common uses for webpages might be:
#+define window
#+define document
### Interactive
# Prompt for a keystroke before exiting.
#+pauseatend
### Files
# Specify which files to lint
# Use "+recurse" to enable recursion (disabled by default).
# To add a set of files, use "+process FileName", "+process Folder\Path\*.js",
# or "+process Folder\Path\*.htm".
#
P.S. Check out my javascript UUID pagewhich, by the way, passes all of the above JSLint checks. :-)
PS 查看我的 javascript UUID 页面,顺便说一下,它通过了上述所有 JSLint 检查。:-)

