【筆記】JavaScript編碼規範- 命名規範

來源:互聯網
上載者:User

標籤: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編碼規範- 命名規範

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.