# 1 Introduction
JavaScript has been widely used in Baidu, especially in browser-side behavior management. The goal of this document is to make JavaScript code style consistent, easy to understand and maintain.
Although this document is designed for JavaScript, when using various JavaScript precompiled languages (such as TypeScript, etc.), the applicable parts should also try to follow the conventions of this document.
2 Code style
1. Structure
- [Mandatory] Use 4 spaces for an indentation level, 2 spaces or tab characters are not allowed.
- [Mandatory] case and default under switch must be indented one more level.
2. Space
- [Mandatory] Binary operators must be surrounded by a space, and spaces are not allowed between unary operators and the operand.
Example:
var a = !arr.length;
a++;
a = b + c;
[Mandatory] There must be a space before the opening curly brace { { used as the start of a code block.
Example:
// good
if (condition) {
}
while (condition) {
}
function funcName() {
}
// bad
if (condition){
}
while (condition){
}
function funcName(){
}
[Mandatory] There must be a space after if / else / for / while / function / switch / do / try / catch / finally keywords.
Example:
// good
if (condition) {
}
while (condition) {
}
(function () {
})();
// bad
if(condition) {
}
while(condition) {
}
(function() {
})();
[Mandatory] At object creation, there must be a space after the : in the property, and no space before the : is allowed.
Example:
// good
var obj = {
a: 1,
b: 2,
c: 3
};
// bad
var obj = {
a : 1,
b:2,
c :3
};
[Mandatory] In function declarations, named function expressions, and function calls, spaces between the function name and ( are not allowed.
// good
function funcName() {
}
var funcName = function funcName() {
};
funcName();
// bad
function funcName () {
}
var funcName = function funcName () {
};
funcName ();
[Mandatory] No spaces allowed before , and ;
// good
callFunc(a, b);
// bad
callFunc(a , b) ;
[Mandatory] In statements such as function calls, function declarations, bracket expressions, property accesses, if / for / while / switch / catch, etc., spaces are not allowed within () and [] immediately within parentheses.
// good
callFunc(param1, param2, param3);
save(this.list[this.indexes[i]]);
needIncream && (variable += increament);
if (num > list.length) {
}
while (len--) {
}
// bad
callFunc( param1, param2, param3 );
save( this.list[ this.indexes[ i ] ] );
needIncreament && ( variable += increament );
if ( num > list.length ) {
}
while ( len-- ) {
}
[Mandatory] For arrays and objects declared on a single line, if they contain elements, spaces within the brackets immediately within {} and [] are not allowed.
Declare arrays and objects containing elements, and only allow them to be written on one line if the form of the inner elements is relatively simple. If the element is complex, it should still be written on a new line.
Example:
// good
var arr1 = [];
var arr2 = [1, 2, 3];
var obj1 = {};
var obj2 = {name: 'obj'};
var obj3 = {
name: 'obj',
age: 20,
sex: 1
};
// bad
var arr1 = [ ];
var arr2 = [ 1, 2, 3 ];
var obj1 = { };
var obj2 = { name: 'obj' };
var obj3 = {name: 'obj', age: 20, sex: 1};
[Mandatory] There must be no extra spaces at the end of the line.
3. Newline
[Mandatory] A line break must be made at the end of each individual statement.
[Mandatory] Each line must not exceed 120 characters.
[Mandatory] Exceptions are allowed for extremely long indivisible code, such as complex regular expressions. Long strings are not among the exceptions.
Example:
// good
if (user.isAuthenticated()
&& user.isInRole('admin')
&& user.hasAuthority('add-admin')
|| user.hasAuthority('delete-admin')
) {
// Code
}
var result = number1 + number2 + number3
+ number4 + number5;
// bad
if (user.isAuthenticated() &&
user.isInRole('admin') &&
user.hasAuthority('add-admin') ||
user.hasAuthority('delete-admin')) {
// Code
}
var result = number1 + number2 + number3 +
number4 + number5;
[Mandatory] In function declaration, function expression, function call, object creation, array creation, for statement, etc., a newline before , or ; is not allowed.
Example:
// good
var obj = {
a: 1,
b: 2,
c: 3
};
foo(
aVeryVeryLongArgument,
anotherVeryLongArgument,
callback
);
// bad
var obj = {
a: 1
, b: 2
, c: 3
};
foo(
aVeryVeryLongArgument
, anotherVeryLongArgument
, callback
);