[Study Notes] JavaScript coding specifications-blank, learning notes javascript
Use a tab to set two (4) spaces. This function can be configured in IDE. According to actual requirements.
// badfunction() {∙∙∙∙var name;}// badfunction() {∙var name;}// goodfunction() {∙∙var name;}
Leave a space before the braces on the left.
// 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'});
In the control statement (if, while etc), leave a space before the left bracket. Do not have spaces before the function parameter list.
// badif(test) {fight ();}// goodif (test) {fight();}// badfunction AAA() {console.log ('AAA!');}// goodfunction AAA() {console.log('AAA!');}
Use spaces to separate operators
// badvar x=y+5;// goodvar x = y + 5;
End code with a line break
// bad(function(global) {// ...stuff...})(this);// good(function(global) {// ...stuff...})(this);↵
Use indentation when calling a long method chain. It can be emphasized that it is a method call rather than a new statement.
// bad$('#items').find('.selected').highlight().end().find('.open').updateCount();
// good$('#items').find('.selected').highlight().end().find('.open').updateCount();
Leave blank lines before statement Blocks
// 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;
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.