/* 变量申明 */ var x = 1; /* 表达式 */ x++; /* do-while */ do { x++; } while (x < 10);
不须要空格的状况:前端
// 一元运算符 和 三元运算符 ++x; y++; z = x ? 1 : 2; // 对象的属性 var a = { b: 1 }; // 数组 var a = [1, 2]; // 运算符 var a = (1 + 2) * 3;
须要空格的状况:segmentfault
var doSomething = function(a, b, c) { // do something }; // '('前无空格 doSomething(item); // 循环 for (i = 0; i < 6; i++) { x++; }
// 变量申明后,须要空行 var x = 1; // 当变量申明位于最后一行时,无需空行 if (x >= 1) { var y = x + 1; } var a = 2; // 须要在此注释以前空行 a++; function b() { // 当注释位于代码块首行时,无需空行 return a; } // 代码块以后须要空行 for (var i = 0; i < 2; i++) { if (true) { return false; } continue; } var obj = { foo: function() { return 1; }, bar: function() { return 2; } };
换行的地方,行末必须有','或者运算符。数组
须要换行的状况:浏览器
不须要换行的状况:函数
var a = { b: 1, c: 2 }; if (condition) { ... } else { ... } try { ... } catch (e) { ... } finally { ... } function test() { ... } var a, foo = 7, b, c, bar = 8; // 判断语句中的条件过多过长 if (boolean1 && boolean2 && boolean3) { // do someting }
单行注释this
if (condition) { // 缩进与下一行代码保持一致 allowed(); }
多行注释spa
/* * one space after '*' */ var x = 1;
文档注释
参考jsdoc,多用于注释函数与类。prototype
/** * @func * @desc 一个带参数的函数 * @param {string} a - 参数a * @param {number} b=1 - 参数b默认值为1 * @param {string} c=1 - 参数c有两种支持的取值</br>1—表示x</br>2—表示xx * @param {object} d - 参数d为一个对象 * @param {string} d.e - 参数d的e属性 * @param {string} d.f - 参数d的f属性 * @param {object[]} g - 参数g为一个对象数组 * @param {string} g.h - 参数g数组中一项的h属性 * @param {string} g.i - 参数g数组中一项的i属性 * @param {string} [j] - 参数j是一个可选参数 */ function foo(a, b, c, d, g, j) { ... }
最外层统一使用单引号。debug
let y = 'foo'; let z = '<div id="test"></div>';
var thisIsMyName; var goodID; var AndroidVersion; var iOSVersion; var MAX_COUNT = 10; function Person(name) { this.name = name; }
var doSomething = function(item) { // do something }; function doSomething(item) { // do something } doSomething(item); // 当即执行函数 (function() { return 1; })(); // inline function var a = [1, 2, function() { ... }]; // 参数间以', '分割 var doSomething = function(a, b, c) { // do something };
下列关键字后必须有大括号(即便代码块的内容只有一行):if, else,for, while, do, switch, try, catch, finally, with。代码规范
// not good if (condition) doSomething(); // good if (condition) { doSomething(); }
适用场景:
不适用场景:
// not good function test(a, b) { if (b === null) { // not mean b is not supply ... } } // good var a = null; if (a === null) { ... }
永远不要直接使用undefined进行变量判断;
使用typeof和字符串'undefined'对变量进行判断。
// not good if (person === undefined) { ... } // good if (typeof person === 'undefined') { ... }
// not good if (a == 1) { a++; } // good if (a === 1) { a++; } // good for (key in obj) { if (obj.hasOwnProperty(key)) { // be sure that obj[key] belongs to the object and was not inherited console.log(obj[key]); } } // not good Array.prototype.count = function(value) { return 4; }; // not good var x = 1; function test() { if (true) { var x = 0; } x += 1; } // not good function test() { console.log(x); var x = 1; } // not good new Person(); // good var person = new Person(); // not good delete(obj.attr); // good delete obj.attr; // not good if (a = 10) { a++; } // not good var a = [1, , , 2, 3]; // not good var nums = []; for (var i = 0; i < 10; i++) { (function(i) { nums[i] = function(j) { return i + j; }; }(i)); } // not good var singleton = new function() { var privateVar; this.publicMethod = function() { privateVar = 1; }; this.publicMethod2 = function() { privateVar = 2; }; };
function Person() { // not good var me = this; // good var _this = this; // good var that = this; // good var self = this; }
前端工程代码规范(一)——命名规则与工程约定
前端工程代码规范(二)——HTML
前端工程代码规范(三)——CSS, SCSS