標籤:javascript
避免單字母名稱,讓名稱具有描述性
// 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.
【筆記】JavaScript編碼規範- 命名規範