These coding conventions are a work-in-progress, and should be considered "DRAFT."
Most of these coding conventions are taken from others, including:
- http://javascript.crockford.com/code.html
- http://code.google.com/p/google-maps-utility-library-v3/wiki/JSCodingConventions
- JavaScript files should be stored in and delivered as .js files
- Variable declarations must start with
var - All variable declarations should be the first statements in a function body
- The use of global variables should be avoided
- If global variables are absolutely necessary, they should be added to the
NPMapnamespace - All JavaScript expressions must end with a semi-colon
- When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself (see Example 1 below)
-
Case:
functionNamesLikeThis,variableNamesLikeThis,ClassNamesLikeThis, andCONSTANTS_LIKE_THIS - Be as descriptive as possible
- Private variables or functions should begin with underscores (_privateFunction, _privateVariable)
- Optional arguments should start with opt_
- Use single quotes, not double ('i am a string')
- Do not use tabs anywhere. Use 2 spaces indent for each level of scope
- Wrapped lines should be indented 4 spaces
- Should follow JSDoc conventions
- Each function must be commented (purpose, @param, @return) - see Example 2 below
- Copyright, author, license, and short description of the file should be at the top of the code - see Example 3 below
1:
var aString = (function() {
return 'i am a string';
});
2:
/**
* A description of this function
* @param {ClassOptions} [opts]
* @return {String}
*/
3:
/**
* @name NameOfClass
* @version 1.1
* @author Nate Irwin
* @copyright (c) 2011 U.S. National Park Service
* @fileoverview A description of this class.
*/
/*
* The code license goes here.
*/