Javascript 風格向導


 
  大部分針對Javascript最合理的方法歸納。
 
類型
 
• 原始類型:我們可以直接使用值。
  ο  string
  ο  number
  ο  boolean
  ο  null
  ο  undefined
var foo = 1,
    bar = foo;

bar = 9;

console.log(foo, bar); // => 1, 9

•   復合類型:我們通過`引用`對值進行間接訪問。

  ο  object

  ο  array

  ο  function

 

var foo = [1, 2],
    bar = foo;

bar[0] = 9;

console.log(foo[0], bar[0]); // => 9, 9

  

Objects
 
• 使用{}創建對象。
// bad
var item = new Object();

// good
var item = {};

 

• 不要使用保留字作為關鍵字。

// bad
var superman = {
  class: 'superhero',
  default: { clark: 'kent' },
  private: true
};

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

 

Arrays
 
 • 使用[]創建數組
// bad
var items = new Array();

// good
var items = [];

 • 如果你不知道數組長度,使用Array#push。

var someStack = [];

// bad
someStack[someStack.length] = 'abracadabra';

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

 

  • 當你需要復制數組的時候,請使用Array#slice。

var len = items.length,
    itemsCopy = [],
    i;

// bad
for (i = 0; i < len; i++) {
  itemsCopy[i] = items[i];
}

// good
itemsCopy = items.slice();

 

Strings

 • 對於字符串,我們使用單引號''。

// bad
var name = "Bob Parr";

// good
var name = 'Bob Parr';

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

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

 • 超過80個字符的字符串,我們使用串聯符號(\),讓字符串多行顯示。

 • 注意:如果過度使用帶串聯符號的字符可能會影響到性能。

 

// bad
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
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.';

// good
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.';

 

  

 • 當我們在編程的時候,需要拼接出一個字符串,我們可以使用Array#join 代替字符串連接。尤其是對IE瀏覽器。 

var items,
    messages,
    length, i;

messages = [{
    state: 'success',
    message: 'This one worked.'
},{
    state: 'success',
    message: 'This one worked as well.'
},{
    state: 'error',
    message: 'This one did not work.'
}];

length = messages.length;

// 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] = messages[i].message;
  }

  return '<ul><li>' + items.join('</li><li>') + '</li></ul>';
}

 

Functions

  • 函數表達式

// anonymous function expression
var anonymous = function() {
  return true;
};

// named function expression
var named = function named() {
  return true;
};

// immediately-invoked function expression (IIFE)
(function() {
  console.log('Welcome to the Internet. Please follow me.');
})();

 • 絕對不要在非函數塊(if,while)申明一個函數。我們可以把函數申明變成一個函數表達式。

// bad
if (currentUser) {
  function test() {
    console.log('Nope.');
  }
}

// good
if (currentUser) {
  var test = function test() {
    console.log('Yup.');
  };
}

 

 • 絕對不要把一個參數命名為arguments,arguments參數是函數作用域內給出的一個特殊變量,如果你把參數命名為arguments,那么這個參數就會覆蓋它原有的特殊變量。

// bad
function nope(name, options, arguments) {
  // ...stuff...
}

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

 

總結
 
  這些很多是大家都比較清楚的,平時經常用,我只是強調一下,讓大家再復習一下。
 

 

推薦
 

 

 
  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM