【笔记】JavaScript编码规范- 命名规范

来源:互联网 发布:净资产收益率排序软件 编辑:程序博客网 时间:2024/05/16 11:29

避免单字母名称,让名称具有描述性

// badfunction q() {// ...stuff...}// goodfunction query() {// ..stuff..}

当命名对象、函数和实例时使用骆驼拼写法

// badvar OBJEcttsssss = {};var this_is_my_object = {};function c() {}var u = new user({name: 'Bob Parr'});// goodvar thisIsMyObject = {};function thisIsMyFunction() {}var user = new User({name: 'Bob Parr'});<strong></strong>
当命名构造函数或类名时,使用驼峰式写法
// badfunction user(options) {this.name = options.name;}var bad = new user({name: 'nope'});// goodfunction User(options) {this.name = options.name;}var good = new User({name: 'yup'});

命名私有属性时使用前置下划线
// badthis.__firstName__ = 'Panda';this.firstName_ = 'Panda';// goodthis._firstName = 'Panda';

保存this引用时使用_this

// badfunction() {var self = this;return function() {console.log(self);};}// badfunction() {var that = this;return function() {console.log(that);};}// goodfunction() {var _this = this;return function() {console.log(_this);};}

命名函数时,下面的方式有利于堆栈跟踪
// badvar log = function(msg) {console.log(msg);};// goodvar log = function log(msg) {console.log(msg);};


如果文件作为一个类被导出,文件名应该和类名保持一致

// file contentsclass CheckBox {// ...}module.exports = CheckBox;// in some other file// badvar CheckBox = require('./checkBox');// badvar CheckBox = require('./check_box');// goodvar CheckBox = require('./CheckBox');

1:15 And let them be for lights in the firmament of the heaven to give light upon the earth:and it was so.

0 0
原创粉丝点击