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);