- Indentation - 4 spaces (do not use tabs)
- Avoid lines longer than 80 characters.
- Required License in File Header
- class - UpperCamelCase
- functions - lowerCamelCase (preferrably verbs)
- variables - lower_case_with_underscores (preferrably nouns)
- constants - ALL_UPPER_CASE_WITH_UNDERSCORES
The standard way of creating classes is the following. Please note that by class we mean an object that is class-like. Also, note the naming of all anonymous functions.
var ClassName = (function ClassNameClosure() {
function ClassName(...) {
...
}
ClassName.prototype = {
functionName: function ClassName_functionName(...) {
...
}
};
return ClassName;
})();
- Declare a variable before it is used
- This helps identify undeclared variables that may become implied globals. Implied global variables should never be used.
- var statements should be the first statements in the function body
- Give each var variables it's own line and comment
- list them in alphabetical order
- Use of global variables should be minimized.
var currentEntry; // currently selected table entry
var level; // indentation level
var size; // size of table
- Always use braces and put them on same line even for single line control statements
if (someVar) {
return true;
} else {
return null;
}
Note: This wasn't always followed, but any new code should do this.
- Space after control statements (if, else, while, for, ...)
if (someVar) {
- Use only strict equalities (and inequalities) in control statements, e.g.
if (someVar === conditionA) {
return true;
} else if (someVar !== conditionB) {
return false;
}
Note: This wasn't always followed, but any new code should do this.
- Liberally comment your code.
- But avoid redundant comments e.g.
i = 0; // Set i to zero.
Its always nice when function names are verbs and variable names are nouns