简体   繁体   English

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

[英]JSLint reports “Unexpected dangling” character in an underscore prefixed variable name

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. 我在我正在构建的网站上使用Google Analytics。 I make reference to GA's variables, such as "_gaq." 我参考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). 我试图让我的JS代码100%JSLint干净(我不喜欢我的编码风格,因此将与Crockford先生的建议一起)。 That said, I can't do anything about Google's variables names... so, I guess I can't get 100% "clean." 也就是说,我对Google的变量名称无能为力......所以,我想我不能100%“干净”。

I post here in case I've misunderstood the message, and can do something to comply with JSLint practices. 我在这里发布,以防我误解了消息,并且可以采取一些措施来遵守JSLint的做法。

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*/

The best way to handle this is just to enable the "Tolerate dangling _ in identifiers" (nomen) option. 处理此问题的最佳方法是启用“容忍悬挂_标识符”(nomen)选项。 See http://www.jslint.com/lint.html for details... 有关详细信息,请访问http://www.jslint.com/lint.html

JSLint is just a code quality tool. JSLint只是一个代码质量工具。 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. 尽管JSLint提出了非常好的建议,但并不总是能够实现它们,特别是当使用未经过测试的其他人的库时。 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. 您应该使用禁用的“禁止悬挂_标识符”选项来检查您的代码,而不是使用无意义的元注释乱丢您的源代码,因为使用您的特定代码似乎没有意义。

I use JSLInt with node.js. 我在node.js中使用JSLInt。 You can pass --nomen flag to get around this feature 你可以通过--nomen flag来绕过这个功能

jslint --nomen myfile.js 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM