Javascript 正確使用方法

來源:互聯網
上載者:User

今天在github 上面找到了一個關於如何正確使用javascript 來進行我們的程式開發.我就恬不知恥的來了個原創啊..坑爹啊.拿來和大家分享一下吧.

A mostly reasonable approach to Javascript.

    Types //類型
    Objects //對象
    Arrays //數組
    Strings //字串
    Functions //函數
    Properties //屬性
    Variables //變數
    Hoisting //變數提升
    Conditional Expressions & Equality //條件運算式和等式.
    Blocks //塊代碼
    Comments //注釋
    Whitespace //空格
    Commas  //逗號
    Semicolons //分號
    Type Casting & Coercion //類型轉換
    Naming Conventions //命名規則
    Accessors //訪問
    Constructors //構造器
    Events //時間
    Modules //模型
    jQuery //
    ECMAScript 5 Compatibility //ECMA 5 相容
    Testing //測試
    Performance //效能
    Resources //資源
    In the Wild
    Translation
    The JavaScript Style Guide Guide
    Contributors
    License


Types (類型)

        原始類型: 當訪問一個原始類型的時候,其實直接存取該原始類型的內容.

    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


object(對象)

使用對象字面量來建立對象 (literal)

//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

};

array(數組)

同樣使用 字面量方法來建立數組

//bad

var items = new Array();

//good

var items = [];

如果你不知道數組的長度,那麼使用Array的內建方法push進行插入操作

var someStack = [];

//bad

someStack[someStack.length] = 'vein';

//good

someStack.push('vein');


當你想要拷貝一個數組的時候,使用array.slice

var len = items.length, //指的就是上面的內容...

  itemCopy = [],

  i;

//bad

for(i = 0; i < len ; ++i){

    itemCopy[i] = items[i];

}

//good

itemCopy = items.slice(); //這裡要注意了.這個我還真不知道...


Strings 字串

使用單引號 (single quotes ) 來包圍字串...//這裡我沒有找到合適的關於效能方面的解釋,我個人也喜歡這麼用,(穿的少總比穿得多好看點吧..你懂得..)

//bad

var name = "Bob Parr";

//good

var name = 'Bob Parr';

//bad

var fullName = "Bob " + this.lastName;

//good

var fullName = 'Bob ' + this.lastName;

字串長於80個字元的時候需要使用字串串連在多行進行編寫..注意,如果過度使用,連接字串將會影響效能(performance)

    // 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 效果會更好..

var items,

    messages,

    length,

    i;

messages = [{

   stat: 'success',

   message: ' This one worked'

},{

  stat: 'success',

   message: ' This one worked'

},{

  stat: 'success',

   message: ' This one worked'

}

];

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)

//匿名函數運算式..

var anonymous = function(){

   return true;

};

// 命名函數運算式.

var named = function named(){

  return true;

};

//即時引用函數

(function(){

  console.log('Welcome to the Internet. Please follow me.');

})();

永遠不要在非函數的塊代碼(if,while)中定義函數.相應的,在代碼塊中間函數賦值給外部的變數名..

//bad

if(currentUser){

  function test(){

    console.log('Nope.');

  }

}

//good

var test;

if(currentUser){

  test = function(){

    console.log('Yup'); 

  };  //be careful with the semi-colon.

}

Properties (屬性)

使用點文法來訪問屬性.

var luke = {

  jedi: true,

  age: 28

};

//bad

var isJedi = luke['jedi'];

//good

var isJedi = luck.jedi;

當使用變數訪問對象屬性時,使用 [] 方括弧來訪問

    var luke = {
      jedi: true,
      age: 28
    };

    function getProp(prop) {
      return luke[prop];
    }

    var isJedi = getProp('jedi');


下一篇接著來....

Best wishes..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.