javascript code style

javascript代碼風格

來源:https://github.com/airbnb/javascriptjavascript

Objects 對象

javascript// bad
var item = new Object();

// good
var item = {};

//不要使用保留字做爲對象屬性,IE8不支持。
// bad
var superman = {
  default: { clark: 'kent' },
  private: true
};

// good
var superman = {
  defaults: { clark: 'kent' },
  hidden: true
};

Arrays 數組

javascript// bad
var items = new Array();

// good
var items = [];

//使用push添加數組元素
var someStack = [];
// bad
someStack[someStack.length] = 'abracadabra';

// good
someStack.push('abracadabra');

//複製數組的最快方法
var len = items.length;
var itemsCopy = [];
var i;
// bad
for (i = 0; i < len; i++) {
  itemsCopy[i] = items[i];
}

// good
itemsCopy = items.slice();

//轉換array-like object(如:{1:'xx',2:'yy',length:2}) 成數組
var args = Array.prototype.slice.call(arguments);

Strings 字符串

javascript//使用單引號
// bad
var name = "Bob Parr";
// good
var name = 'Bob Parr';

// bad
var fullName = "Bob " + this.lastName;
// good
var fullName = 'Bob ' + this.lastName;

// 使用`+`進行換行
var errorMessage = 'This is a super long error that was thrown because ' +
  'of Batman. When you stop to think about how Batman had anything to do ' +
  'with this, you would get nowhere fast.';

//拼接帶標籤內容時,標籤頭尾寫一塊兒
// bad
function inbox(messages) {
  items = '

<ul>';

  for (i = 0; i < length; i++) {
    items += '<li>' + messages[i].message + '</li>';
  }

  return items + '</ul>

';
}

// good
function inbox(messages) {
  items = [];

  for (i = 0; i < length; i++) {
    items[i] = '<li>' + messages[i].message + '</li>';
  }

  return '

<ul>' + items.join('') + '</ul>

';
}

Functions 函數

javascript// 使用自執行函數
// immediately-invoked function expression (IIFE)
(function() {
  console.log('Welcome to the Internet. Please follow me.');
})();

//型參別用`arguments`
// bad
function nope(name, options, arguments) {
  // ...stuff...
}

// good
function yup(name, options, args) {
  // ...stuff...
}

Properties屬性

javascript
var luke = { jedi: true, age: 28 }; // bad var isJedi = luke['jedi']; //通常狀況使用`.`調用, // good var isJedi = luke.jedi; function getProp(prop) { //屬性是變量,使用`[]`調用 return luke[prop]; } var isJedi = getProp('jedi');

Variables 變量

javascript
//使用var聲明變量 // bad superPower = new SuperPower(); // good var superPower = new SuperPower(); //不用`,`分割變量 // bad // (compare to above, and try to spot the mistake) var items = getItems(), goSportsTeam = true; dragonball = 'z'; // good var items = getItems(); var goSportsTeam = true; var dragonball = 'z'; //聲明未賦值變量在後面 // bad var i; var items = getItems(); var dragonball; var goSportsTeam = true; var len; // good var items = getItems(); var goSportsTeam = true; var dragonball; var length; var i; //函數內變量聲明定義儘可能放最前面,除非有函數開始有判斷`return false`, // bad function() { test(); console.log('doing stuff..'); //..other stuff.. var name = getName(); if (name === 'test') { return false; } return name; } // good function() { var name = getName(); test(); console.log('doing stuff..'); //..other stuff.. if (name === 'test') { return false; } return name; } // bad function() { var name = getName(); if (!arguments.length) { return false; } return true; } // good function() { if (!arguments.length) { return false; } var name = getName(); return true; }

Equality 比較操做

javascript// bad
if (name !== '') {
  // ...stuff...
}

// good
if (name) {
  // ...stuff...
}

// bad
if (collection.length > 0) {
  // ...stuff...
}

// good
if (collection.length) {
  // ...stuff...
}

Blocks 塊

javascript// bad
if (test)
  return false;

// good
if (test) return false;

// good
if (test) {
  return false;
}

// bad
function() { return false; }

// good
function() {
  return false;
}

Comments 註釋

javascript// bad
// make() returns a new element
// based on the passed in tag name
//
// @param {String} tag
// @return {Element} element
function make(tag) {

  // ...stuff...

  return element;
}

// good
/**
 * make() returns a new element
 * based on the passed in tag name
 *
 * @param {String} tag
 * @return {Element} element
 */
function make(tag) {

  // ...stuff...

  return element;
}


// bad
var active = true;  // is current tab

//註釋在在代碼上方好
// good
// is current tab
var active = true;

// bad
function getType() {
  console.log('fetching type...');
  // set the default type to 'no type'
  var type = this._type || 'no type';

  return type;
}

// good
function getType() {
  console.log('fetching type...');

  //註釋上方要留一行空行
  // set the default type to 'no type'
  var type = this._type || 'no type';

  return type;
}

Type Casting 格式轉換

javascript//  => this.reviewScore = 9;

// bad
var totalScore = this.reviewScore + '';

// good
var totalScore = '' + this.reviewScore;

// bad
var totalScore = '' + this.reviewScore + ' total score';

// good
var totalScore = this.reviewScore + ' total score';

var inputValue = '4';

// bad
var val = new Number(inputValue);

// bad
var val = +inputValue;

// bad
var val = inputValue >> 0;

// bad
var val = parseInt(inputValue);

// good
var val = Number(inputValue);

// good
var val = parseInt(inputValue, 10);

// good
/**
 * parseInt was the reason my code was slow.
 * Bitshifting the String to coerce it to a
 * Number made it a lot faster.
 * 經過位移的方式轉換成數字,速度會快些
 */
var val = inputValue >> 0;

var age = 0;

// bad
var hasAge = new Boolean(age);

// good
var hasAge = Boolean(age);

// good
var hasAge = !!age;

Naming Conventions 命名約定

javascript// bad
function q() {
  // ...stuff...
}

//名字要有語意
// good
function query() {
  // ..stuff..
}

// bad
var OBJEcttsssss = {};
var this_is_my_object = {};

//駝峯命名
// good
var thisIsMyObject = {};
function thisIsMyFunction() {}

// bad
function user(options) {
  this.name = options.name;
}

//構造函數大寫
// good
function User(options) {
  this.name = options.name;
}

// bad
this.__firstName__ = 'Panda';
this.firstName_ = 'Panda';

//私有屬性用`_`開頭
// good
this._firstName = 'Panda';

// bad
function() {
  var self = this;
  return function() {
    console.log(self);
  };
}

// bad
function() {
  var that = this;
  return function() {
    console.log(that);
  };
}

//保存調用者`this`爲`_this`
// good
function() {
  var _this = this;
  return function() {
    console.log(_this);
  };
}

Constructors 構造函數

javascriptfunction Jedi() {
  console.log('new jedi');
}

// bad
Jedi.prototype = {
  fight: function fight() {
    console.log('fighting');
  }
};

// good
Jedi.prototype.fight = function fight() {
  console.log('fighting');
};

Events 事件

javascript// bad
$(this).trigger('listingUpdated', listing.id);

...

$(this).on('listingUpdated', function(e, listingId) {
  // do something with listingId
});

//傳遞id時用對象包裹後再傳
// good
$(this).trigger('listingUpdated', { listingId : listing.id });

...

$(this).on('listingUpdated', function(e, data) {
  // do something with data.listingId
});

Modules 模塊

  • 說明文件路徑
  • 文件名使用駝峯命名
  • 開頭加上一個 `!
  • 模塊中使用'use strict'嚴格模式
  • 使用noConflict()無衝突的輸出
javascript// 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);
相關文章
相關標籤/搜索