【学习笔记】JavaScript编码规范- 空白
2015-05-19 09:04
489 查看
使用制表符设置两(四)个空格,此功能一般在IDE中可配置。具体可根据实际要求。
在左侧大括号前面保留一个空格。
在控制语句中(if, while etc),左括号之前留一个空格。函数的参数列表之前不要有空格.
使用空白来分隔运算符
使用换行符结束代码
当调用很长的方法链时使用缩进,可强调是方法调用,而不是新的语句。
在语句块之前保留空行
1:12 And the earth brought forth grass ,and herb yielding seed after his kind,and the tree yielding fruit,whose seed was in itself,after his kind;and God saw that it was good.
// bad function() { ∙∙∙∙var name; } // bad function() { ∙var name; } // good function() { ∙∙var name; }
在左侧大括号前面保留一个空格。
// bad function test(){ console.log('test'); } // good function test() { console.log('test'); } // bad dog.set('attr',{ age: '1 year', breed: 'Samoyed ' }); // good dog.set('attr', { age: '1 year', breed: 'Husky' });
在控制语句中(if, while etc),左括号之前留一个空格。函数的参数列表之前不要有空格.
// bad if(test) { fight (); } // good if (test) { fight(); } // bad function AAA() { console.log ('AAA!'); } // good function AAA() { console.log('AAA!'); }
使用空白来分隔运算符
// bad var x=y+5; // good var x = y + 5;
使用换行符结束代码
// bad (function(global) { // ...stuff... })(this); // good (function(global) { // ...stuff... })(this);↵
当调用很长的方法链时使用缩进,可强调是方法调用,而不是新的语句。
// bad $('#items').find('.selected').highlight().end().find('.open').updateCount();
// good $('#items') .find('.selected') .highlight() .end() .find('.open') .updateCount();
在语句块之前保留空行
// bad if (test) { return A; } return A; // good if (test) { return A; } return A; // bad var obj = { A: function() { }, B: function() { } }; return obj; // good var obj = { A: function() { }, B: function() { } }; return obj;
1:12 And the earth brought forth grass ,and herb yielding seed after his kind,and the tree yielding fruit,whose seed was in itself,after his kind;and God saw that it was good.
相关文章推荐
- 【学习笔记】JavaScript编码规范- 注释
- 【学习笔记】JavaScript编码规范-数组
- 【学习笔记】JavaScript编码规范-字符串
- 【学习笔记】JavaScript编码规范-语句块
- 【学习笔记】JavaScript编码规范-声明提升
- 【学习笔记】JavaScript编码规范-比较运算符&相等
- 【学习笔记】JavaScript编码规范-变量
- 【学习笔记】JavaScript编码规范-类型
- 【学习笔记】JavaScript编码规范-对象
- 【学习笔记】JavaScript编码规范-函数
- 【学习笔记】JavaScript编码规范-属性
- Kotlin 学习笔记(二) 编码规范和导入包
- 【笔记】JavaScript编码规范- 命名规范
- Jquery学习笔记——编码规范
- 【笔记】JavaScript编码规范- 存取器
- 【笔记】JavaScript编码规范- jQuery
- C++ FAQ学习笔记 27章 编码规范
- 学习笔记:如何写出规范而优美的JavaScript代码(20180218-2)
- 【笔记】JavaScript编码规范- 逗号和分号
- 【笔记】JavaScript编码规范- 构造函数