Andreas Grech
Andreas Grech

Reputation: 107950

JavaScript: 'Disallow leading _ in identifiers' as an option in JSLint

I have just started writing my own JavaScript Framework (just for the learning experience), and have prefixed some private members with a _, like such:

var _isFireBugEnabled = function () {
    return (window.console && window.console.firebug);
};

When I ran my code against Crockford's JSLint (as always), with Recommended Options on, I was told about not using a _ as an identifier.

My question is, Why does JSLint warn me as regards not using a _ as an identifier?

Are there some side effects or implications I am missing here?

PS. As far as I could scan just now, this is not documented in the book

Upvotes: 1

Views: 990

Answers (2)

Kenan Banks
Kenan Banks

Reputation: 211980

The reason is that Douglas Crockford hates about 78% of Javascript*. Many people think he's a bit strict, and in fact many libraries do use leading underscores in production code. I don't see much wrong with it. There are no side effects.

Additionally, the '$', not the underscore, was the symbol set aside for "system" code by the ECMA spec.

from ECMA 262, section 7.6:

This standard specifies one departure from the grammar given in the Unicode standard: The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code.

*Note: I'm being facetious. He really only hates about half, and he typically has good reason. I'd disagree with Crockford here, but he's usually very right.

Upvotes: 13

foxxtrot
foxxtrot

Reputation: 11402

I've actually e-mailed Crockford on this. This was his response:

I think _ should be reserved for system code implementation, and not used by applications.

I disagree with him somewhat, I tend to use _ to prefix truly private members in my own classes, because it makes it clear to me what is private. Google's Caja has some rules regarding the use of _, but nothing that should cause problems with what you're describing.

Upvotes: 8

Related Questions