73

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.

I use Google Analytics on a Web site I am building. I make reference to GA's variables, such as "_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."

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
Zhami
  • 19,033
  • 14
  • 48
  • 47

4 Answers4

72

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 nomen: true*/
... statement(s) with _var ...
/*jslint nomen: false*/
Ulflander
  • 648
  • 5
  • 15
Zhami
  • 19,033
  • 14
  • 48
  • 47
  • 12
    -1. To disable this warning you need to set nomen: to true first, then false. From jslint website: "true if names should not be checked for initial or trailing underbars." – Igor Zevaka Apr 11 '12 at 05:47
  • 3
    This doesn't comply with JSLint practices; it disables checking for that warning. – WhyNotHugo Jul 25 '12 at 21:43
  • 3
    This will get messy if you're using a library like underscore.js. Use `jslint --nomen ` instead. – pseudosudo May 06 '14 at 19:41
51

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

scruffian
  • 697
  • 6
  • 7
  • 7
    this is the best answer for the question asked. I wish jslint would tolerate underscorejs but not _ identifiers... – John Dhom Jun 05 '12 at 19:26
  • 1
    Could you please provide a working link? I'm somehow confused how to find the appropriated resources. – OddDev Jun 08 '15 at 07:17
  • The links in the answer and the comments are broken. Better to include the answer here than to link to it – Tim Partridge Feb 21 '19 at 19:50
15

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.

  • 1
    I appreciate your perspective, and while JSLint is one person's vision, it has worked well for me to have my library of code be 100% clean -- when I have bugs, they are always in logic. – Zhami Jun 06 '11 at 21:49
  • 1
    My main point is that the JSLint meta-comments tend to reduce readability and would probably make another reader go "Huh?", so it would be a better idea to either ignore or disable that particular check than to keep them in your source. –  Jun 07 '11 at 08:09
  • 2
    And what about other code which doesn't depend on the offending external library? This is what the /*jslint directives are for. It's not a decision you have to make globally. – 1983 Jul 14 '12 at 20:17
8

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

jslint --nomen myfile.js 
Jaseem
  • 2,236
  • 6
  • 28
  • 35