Javascript JSLint 在下划线前缀变量名中报告“意外悬空”字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3039587/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:03:03  来源:igfitidea点击:

JSLint reports "Unexpected dangling" character in an underscore prefixed variable name

javascriptjslint

提问by Zhami

I know that some people consider the presence of a leading underscore to imply that a variable is "private," that such privacy is a fiction, and assume this is why JSLint reports such names with an error message.

我知道有些人认为前导下划线的存在暗示变量是“私有的”,这种隐私是虚构的,并假设这就是 JSLint 报告此类名称并显示错误消息的原因。

I use Google Analytics on a Web site I am building. I make reference to GA's variables, such as "_gaq."

我在我正在构建的网站上使用 Google Analytics。我参考了 GA 的变量,例如“_gaq”。

I am trying to get my JS code to be 100% JSLint clean (I'm not religious about my coding style, and so will go with Mr. Crockford's counsel). That said, I can't do anything about Google's variables names... so, I guess I can't get 100% "clean."

我试图让我的 JS 代码 100% JSLint 干净(我对我的编码风格并不虔诚,所以会和 Crockford 先生的建议一起去)。也就是说,我对 Google 的变量名称无能为力……所以,我想我无法 100%“干净”。

I post here in case I've misunderstood the message, and can do something to comply with JSLint practices.

我在这里发布以防我误解了该消息,并且可以做一些事情来遵守 JSLint 实践。

采纳答案by Zhami

Ah, I've got this handled... I wrap the statements that use the underscore prefixed variables with JSLint commands to disable, then re-enable this class of error:

啊,我已经解决了这个问题......我用 JSLint 命令将使用下划线前缀变量的语句包装起来以禁用,然后重新启用此类错误:

/*jslint nomen: true*/
... statement(s) with _var ...
/*jslint nomen: false*/

回答by scruffian

The best way to handle this is just to enable the "Tolerate dangling _ in identifiers" (nomen) option. See http://www.jslint.com/lint.htmlfor details...

处理此问题的最佳方法是启用“容忍标识符中的悬空 _”(nomen)选项。有关详细信息,请参阅http://www.jslint.com/lint.html...

回答by scruffian

JSLint is just a code quality tool. Not completely passing its tests does not mean your code is bad; it simply means you don't follow all the conventions laid out by its creator. Although JSLint makes very good suggestions, it is not always possible to fulfill them all, especially when using someone else's library which was not tested against it. Rather than littering your source code with meaningless meta-comments, you should check your code with the "Disallow dangling _ in identifiers" option disabled, since it seems not to makes sense to use with your particular code.

JSLint 只是一个代码质量工具。没有完全通过测试并不意味着你的代码很糟糕;它只是意味着您没有遵循其创建者制定的所有约定。尽管 JSLint 提出了很好的建议,但并不总是能够实现所有建议,尤其是在使用未经测试的其他人的库时。与其用无意义的元注释散布源代码,您应该检查您的代码并禁用“禁止在标识符中悬挂 _”选项,因为与您的特定代码一起使用似乎没有意义。

回答by Jaseem

I use JSLInt with node.js. You can pass --nomen flag to get around this feature

我将 JSLInt 与 node.js 一起使用。您可以通过 --nomen 标志来绕过此功能

jslint --nomen myfile.js