2. Eslint and JSCS tools
3. Additional formatting standards
Anonymous function calls
myObject.myFunction(param1, function (a,b) {
//Function logic
return a > b;
});
End of file
This reduces the quantity of the changed lines in a diff and makes code safer in file concatenation processes.
Indentation
Tabs are not allowed as indentation.
Wrapped lines
Max line length
Line termination
Multi-line string literals
‘Battling with Microsoft over the Internet, Netscape considered their client-server solution ‘ +
‘as a distributed OS, running a portable version of Sun Microsystem’s Java. ‘ +
‘Because Java was a competitor of C++ and aimed at professional programmers, ‘ +
‘Netscape also wanted a lightweight interpreted language that would complement Java ‘ +
‘by appealing to nonprofessional programmers, like Microsoft’s VB.[9] (see JavaScript and Java)’;
Parentheses
Never use parentheses for:
- Unary operators (e.g. delete, typeof, and void)
- After keywords such as return, throw
- For case, in, or new, and others keywords like them.
Blocks
// Wrong
if (true)
blah();function () { return false; }// Correct
if (true) return;if (true) {
return;
}if (true) {
blah();
}
function () {
return false;
}
Semicolons
// Example 1: JavaScript Error
MyClass.prototype.myMethod = function() {
return 42;
} // <-- Missing semicolon(function() {
// Some initialization code wrapped in a function to create a scope for locals.
})();
// Example 2: Trying to do one thing on Internet Explorer and another on Firefox.
var x = {
'i': 1,
'j': 2
} // <-- Missing semicolon[normalVersion, ffVersion][isIE]();
// Example 3: Conditional execution a la bash
var THINGS_TO_EAT = [apples, oysters, sprayOnCheese] // <-- Missing semicolon-1 == resultOfOperation() || die();
Explanation
Strings
4. Additional naming convention standards
General naming conventions
- Avoid underscores and numbers in names.
- Variables or methods should have names that accurately describe their purpose or behavior.
- Object methods or variables that are declared private or protected should start with an underscore(_)
Functions and methods
- Class method names should start with an English verb in its infinitive form that describes the method.
- Names for accessors for instance or static variables should always have the get or set prefix.
- In design pattern classes, implementation method names should contain the pattern name where practical to provide better behavior description.
- Methods that return status flags or Boolean values should have the has or is prefix
Variables and properties
- Do not use short variable names such as i or n except in small loop contexts
- If a loop contains more than 20 lines of code, the index variables should have more descriptive names.
5. Additional coding construct standards
Binary and ternary operators
Custom toString() method
Function declarations within blocks
// Wrong
if (x) {
function foo() {}
}// Correct
if (x) {
Exceptions and custom exceptions
Standard features
For example, string.charAt(3) instead of string[3], and element access with DOM functions instead of using an application-specific shorthand.
Method definitions
Foo.prototype.bar = function() {
// ...
};
Foo.prototype = {
bar: function() {
// ...
},
circle: function() {
// ...
}
};
Closures
// Wrong
function foo(element, a, b) {
element.onclick = function() {
// uses a and b
};
}
Because elements also keep references to the closure, it is a cycle that will not be cleaned up by garbage collection. In these situations, the code can be structured as follows:
// Correct
function foo(element, a, b) {
element.onclick = bar(a, b);
}function bar(a, b) {
return function() {
// uses a and b
}
}
6. Additional general standards
Array and object initializers
var obj = {a: 1, b: 2, c: 3}; // No space after { or before }.
Object.prototype = {
a: 0,
b: 1,
lengthyName: 2
};
Associative arrays
Deferred initialization
Explicit scope
Built-in objects
Variable declarations
var foo = 'bar',
num = 1,
arr = [1, 2, 3];