JavaScript naming conventions
I know there is a lot of controversy (maybe not controversy, but arguments at least) about which naming convention is the best for JavaScript.
How do you name your variables, functions, objects and such?
I'll leave my own thoughts on this out, as I haven't been doing JS for long (couple of years, only), and I just got a request to create a document with naming conventions to be used in our projects at work. So I've been looking (google-ing) around, and there are so many different opinions.
The books I've read on JS also use different naming conventions themselves, but they all agree on one bit: “Find what suits you, and stick to it.” But now that I've read so much around, I found that I like some of the other methods a bit better than what I'm used to now.
I follow Douglas Crockford's code conventions for javascript. I also use his JSLint tool to validate following those conventions.
As Geoff says, what Crockford says is good.
The only exception I follow (and have seen widely used) is to use $varname to indicate a jQuery (or whatever library) object. Eg
var footer = document.getElementById('footer');
var $footer = $('#footer');
You can follow this Google JavaScript Style Guide
In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, and SYMBOLIC_CONSTANTS_LIKE_THIS.
EDIT: See nice collection of JavaScript Style Guides And Beautifiers.
链接地址: http://www.djcxy.com/p/67814.html上一篇: Codeigniter:在Mysql中插入重音的问题
下一篇: JavaScript命名约定