序
繼續前兩篇,這篇作為終結篇。
Blocks
• 有{}的代碼,我們換行處理。
// bad if (test) return false; // good if (test) return false; // good if (test) { return false; } // bad function() { return false; } // good function() { return false; }
Comments
• 對於多行注釋使用/** ... */。包含描述信息、參數類型和返回值。
// bad // make() returns a new element // based on the passed in tag name // // @param <String> tag // @return <Element> element function make(tag) { // ...stuff... return element; } // good /** * make() returns a new element * based on the passed in tag name * * @param <String> tag * @return <Element> element */ function make(tag) { // ...stuff... return element; }
• 對於單行注釋使用//。單行注釋單獨放置在一個新行上。在注釋前面放置一個空行。
// bad var active = true; // is current tab // good // is current tab var active = true; // bad function getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type; } // good function getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type; }
• 對於一些問題,注釋前加FIXME或TODO,這樣將快速幫助開發者快速明白代碼意圖。
• 使用 // FIXME: 注釋問題
function Calculator() { // FIXME: shouldn't use a global here total = 0; return this; }
• 使用 // TODO: 注釋問題的解決方案
function Calculator() { // TODO: total should be configurable by an options param this.total = 0; return this; }
Type Casting & Coercion
• 在聲明之前執行強制類型轉換。
• 字符串
// => this.reviewScore = 9; // bad var totalScore = this.reviewScore + ''; // good var totalScore = '' + this.reviewScore; // bad var totalScore = '' + this.reviewScore + ' total score'; // good var totalScore = this.reviewScore + ' total score';
• 對於數字轉換,使用parseInt,而且要帶着類型轉換的基數。
• 如果parseInt成為你的瓶頸,處於性能原因,需要你使用“位移”操作。那么請寫下注釋解釋你這樣做的原因。
var inputValue = '4'; // bad var val = new Number(inputValue); // bad var val = +inputValue; // bad var val = inputValue >> 0; // bad var val = parseInt(inputValue); // good var val = Number(inputValue); // good var val = parseInt(inputValue, 10); // good /** * parseInt 使我的代碼變慢. * 為了提高速度,使用位移操作讓字符串強制轉化為數字。 */ var val = inputValue >> 0;
• 布爾
var age = 0; // bad var hasAge = new Boolean(age); // good var hasAge = Boolean(age); // good var hasAge = !!age;
Constructors
• 用方法擴展對象,而不是用一個新對象。
function Jedi() { console.log('new jedi'); } // bad Jedi.prototype = { fight: function fight() { console.log('fighting'); }, block: function block() { console.log('blocking'); } }; // good Jedi.prototype.fight = function fight() { console.log('fighting'); }; Jedi.prototype.block = function block() { console.log('blocking'); };
• 讓對象的方法return this,這樣有利於方法的鏈鎖操作。
// bad Jedi.prototype.jump = function() { this.jumping = true; return true; }; Jedi.prototype.setHeight = function(height) { this.height = height; }; var luke = new Jedi(); luke.jump(); // => true luke.setHeight(20) // => undefined // good Jedi.prototype.jump = function() { this.jumping = true; return this; }; Jedi.prototype.setHeight = function(height) { this.height = height; return this; }; var luke = new Jedi(); luke.jump() .setHeight(20);
• 我們可以自定義一個toString()方法。——要確保它能正常運行,而且不會產生其他影響。
function Jedi(options) { options || (options = {}); this.name = options.name || 'no name'; } Jedi.prototype.getName = function getName() { return this.name; }; Jedi.prototype.toString = function toString() { return 'Jedi - ' + this.getName(); };
總結
終於算是寫完了,希望能夠對大家所有幫助。
推薦
