【学习笔记】JavaScript编码规范- 空白

来源:互联网 发布:魔方淘宝 编辑:程序博客网 时间:2024/06/10 10:23

使用制表符设置两(四)个空格,此功能一般在IDE中可配置。具体可根据实际要求。

// badfunction() {∙∙∙∙var name;}// badfunction() {∙var name;}// goodfunction() {∙∙var name;}


在左侧大括号前面保留一个空格。
// badfunction test(){console.log('test');}// goodfunction test() {console.log('test');}// baddog.set('attr',{age: '1 year',breed: 'Samoyed'});// gooddog.set('attr', {age: '1 year',breed: 'Husky'});


在控制语句中(if, while etc),左括号之前留一个空格。函数的参数列表之前不要有空格.

// badif(test) {fight ();}// goodif (test) {fight();}// badfunction AAA() {console.log ('AAA!');}// goodfunction AAA() {console.log('AAA!');}

使用空白来分隔运算符

// badvar x=y+5;// goodvar 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();

在语句块之前保留空行

// badif (test) {return A;}return A;// goodif (test) {return A;}return A;// badvar obj = {A: function() {},B: function() {}};return obj;// goodvar 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.

0 0