Esto eliminará la página "Mr.doob's-Code-Style™"
. Por favor, asegúrate de que es lo que quieres.
See the online mrdoobapproves code beautifier.
This style guide is also available as an eslint plugin on NPM.
Naming should be as descriptive as possible. The only exception is the indexing variable in a loop. That can be shortened to a single letter starting from i.
Private properties and methods of objects begin with an underscore _.
{}
and not new Object()
.{}
without spaces.good:
var obj = { A: 1, b: 2, C: 3 };
var obj = {
A: 1,
b: 2,
C: 3
};
poor:
var obj = {A:1,b:2,C:3};
var obj = {A:1, b:2, C:3};
var obj = {A : 1, b : 2, C : 3};
var obj = { "A" : 1, "b" : 2, "C" : 3 };
var obj = { A : 1, b : 2, C : 3 };
var obj = { A :1, b :2, C :3 };
var obj = { A : 1 , b : 2 , C : 3 };
var obj = {
A : 1,
b : 2,
C : 3,
};
var obj = {
A : 1
, b : 2
, C : 3
};
[]
and not new Array()
.[]
without spaces.good:
var arr = [ 1, 2, 3 ];
var arr = [
1,
2,
3
];
poor:
var arr = [1,2,3];
var arr = [1, 2, 3];
var arr = [ 1 , 2 , 3 ];
var arr = [
1,
2,
3,
];
var arr = [
1
, 2
, 3
];
Strings are written using single quotes:
good:
var lyrics = 'Never gonna Give you up, Never gonna Let you down' ;
Semicolons are always placed.
good:
if ( a === 0 ) {
// this is good
return true;
}
poor:
if ( a === 0 ) {
// this is bad: missing empty line after '{'
return true;
}
if ( a === 0 ) {
// this is bad: missing empty line before '}'
return true;
}
if ( a === 0 ) { // this is bad: stuff after '{'
return true;
}
if ( a === 0 ){
// this is bad: no space before '{'
return true;
}
good:
if ( test ) {
// ...
} else {
// ...
}
poor:
if (test) {
// ...
}
if( test ) {
// ...
}
if (test ) {
// ...
}
if ( test) {
// ...
}
if ( test ){
// ...
}
if ( test ) {
// ...
}else {
// ...
}
if ( test ) {
// ...
} else{
// ...
}
good:
switch ( value ) {
case 1 :
// ...
break ;
case 2 :
// ...
break ;
default :
// ...
// no break keyword on the last case
}
foreach
and for in
are much slower than for
.Operator with
not used.
Always use strict equality ===
(inequality !==
).
Always use spaces around the colon and question mark.
All unary operators are written together with the following operands:
var foo = ! bar;
Avoid using eval. To parse json, use JSON.parse.
Check the value through a strict comparison.
Good:
x === undefined;
Poor:
/ / In modern browsers already defined immutable undefined.
var undefined;
x === undefined;
typeof x === 'undefined'
x === void 0
Esto eliminará la página "Mr.doob's-Code-Style™"
. Por favor, asegúrate de que es lo que quieres.