ECMAScript 規範中標識符采用駝峯大小寫格式,駝峯命名法由小(大)寫字母開始,後續每一個單詞首字母都大寫。根據首字母是否大寫,分爲兩種方式:ajax
標識符,則包括變量、函數名、類名、屬性名和函數或類的參數,每一個命名方法又略有不一樣,下面詳細解釋一下:數組
所有采用小寫方式, 如下劃線分隔。函數
示例:my_project_name測試
參照項目命名規則;有複數結構時,要採用複數命名法。ui
示例:scripts, styles, images, data_modelsthis
命名規範:前綴應當是名詞。(函數的名字前綴爲動詞,以此區分變量和函數)url
命名建議:儘可能在變量名字中體現所屬類型,如:length、count等表示數字類型;而包含name、title表示爲字符串類型。prototype
// 好的命名方式 let maxCount = 10; let tableTitle = 'LoginTable'; // 很差的命名方式 let setCount = 10; let getTitle = 'LoginTable';
命名規範:使用大寫字母和下劃線來組合命名,下劃線用以分割單詞。code
const MAX_COUNT = 10; const URL = 'http://www.foreverz.com';
動詞 | 含義 | 返回值 |
---|---|---|
can | 判斷是否可執行某個動做(權限) | 函數返回一個布爾值。true:可執行;false:不可執行 |
has | 判斷是否含有某個值 | 函數返回一個布爾值。true:含有此值;false:不含有此值 |
is | 判斷是否爲某個值 | 函數返回一個布爾值。true:爲某個值;false:不爲某個值 |
get | 獲取某個值 | 函數返回一個非布爾值 |
set | 設置某個值 | 無返回值、返回是否設置成功或者返回鏈式對象 |
load | 加載某些數據 | 無返回值或者返回是否加載完成的結果 |
// 是否可閱讀 function canRead(): boolean { return true; } // 獲取名稱 function getName(): string { return this.name; }
命名方法:大駝峯式命名法,首字母大寫。對象
命名規範:前綴爲名稱。
示例:
class Person { public name: string; constructor(name) { this.name = name; } } const person = new Person('mevyn');
類的成員包含:
示例:
class Person { private _name: string; constructor() { } // 公共方法 getName() { return this._name; } // 公共方法 setName(name) { this._name = name; } } const person = new Person(); person.setName('mervyn'); person.getName(); // ->mervyn
js 支持三種不一樣類型的註釋:行內註釋、單行註釋和多行註釋:
命名建議:
// 用來顯示一個解釋的評論
// -> 用來顯示錶達式的結果,
// >用來顯示 console 的輸出結果,
示例:
function test() { // 測試函數 console.log('Hello World!'); // >Hello World! return 3 + 2; // ->5 }
示例:
// 調用了一個函數;1)單獨在一行 setTitle();
示例:
/* * 代碼執行到這裏後會調用setTitle()函數 * setTitle():設置title的值 */ setTitle();
說明:函數(方法)註釋也是多行註釋的一種,可是包含了特殊的註釋要求,參照JSDoc
/** * 函數說明 * @關鍵字 */
經常使用註釋關鍵字:(只列出一部分,並非所有)
註釋名 | 語法 | 含義 | 示例 |
---|---|---|---|
@param | @param 參數名 {參數類型} 描述信息 | 描述參數的信息 | @param name {String} 傳入名稱 |
@return | @return {返回類型} 描述信息 | 描述返回值的信息 | @return {Boolean} true:可執行;false:不可執行 |
@author | @author 做者信息 [附屬信息:如郵箱、日期] | 描述此函數做者的信息 | @author 張三 2015/07/21 |
@version | @version XX.XX.XX | 描述此函數的版本號 | @version 1.0.3 |
@example | @example 示例代碼 | 演示函數的使用 | @example setTitle(‘測試’) |
/** * 合併Grid的行 * @param grid {Ext.Grid.Panel} 須要合併的Grid * @param cols {Array} 須要合併列的Index(序號)數組;從0開始計數,序號也包含。 * @param isAllSome {Boolean} :是否2個tr的cols必須完成同樣才能進行合併。true:完成同樣;false(默認):不徹底同樣 * @return void * @author polk6 2015/07/21 * @example * _________________ _________________ * | 年齡 | 姓名 | | 年齡 | 姓名 | * ----------------- mergeCells(grid,[0]) ----------------- * | 18 | 張三 | => | | 張三 | * ----------------- - 18 --------- * | 18 | 王五 | | | 王五 | * ----------------- ----------------- */ function mergeCells(grid: Ext.Grid.Panel, cols: Number[], isAllSome: boolean = false) { // Do Something }
// bad function () { var self = this; return function () { console.log(self); }; } // bad function () { var that = this; return function () { console.log(that); }; } // bad function () { var _this = this; return function () { console.log(_this); }; } // good function () { return function () { console.log(this); }.bind(this); }
// bad var log = function (msg) { console.log(msg); }; // good var log = function log(msg) { console.log(msg); };
// file contents class CheckBox { // ... } module.exports = CheckBox; // in some other file // bad var CheckBox = require('./checkBox'); // bad var CheckBox = require('./check_box'); // good var CheckBox = require('./CheckBox');
// fancyInput/fancyInput.js !function (global) { 'use strict'; var previousFancyInput = global.FancyInput; function FancyInput(options) { this.options = options || {}; } FancyInput.noConflict = function noConflict() { global.FancyInput = previousFancyInput; return FancyInput; }; global.FancyInput = FancyInput; }(this);
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'); };
// bad Jedi.prototype.jump = function jump() { this.jumping = true; return true; }; Jedi.prototype.setHeight = function setHeight(height) { this.height = height; }; var luke = new Jedi(); luke.jump(); // => true luke.setHeight(20); // => undefined // good Jedi.prototype.jump = function jump() { this.jumping = true; return this; }; Jedi.prototype.setHeight = function setHeight(height) { this.height = height; return this; }; var luke = new Jedi(); luke.jump() .setHeight(20);