JavaScript Patterns and Good Parts

來源:互聯網
上載者:User

學習一種語言,自然要看看市面上的好書是一種捷徑。這幾天,我抽空看了看JavaScript Patterns和JavaScript good parts兩本書,算是知識儲備吧。

 

#ASI (Automatic Semicolon insert)

 JavaScript把自己搞得很只能,如果程式碼末尾少了“;”,他還會擇機給你加上一個。JavaScript Interpreter又不是神,時常會犯錯誤,所以通常我們不給他ASI爽一把的機會。

// Goodif (XX) {    // ....}// Bad.// Piaoger以前喜歡這種方式,看來得改改了,// 爭取寫C/C++還有JavaScritpt時代碼風格相似吧。if (XX){    // ....}//Good if( color === RED ||    color === BLUE) {    // ....}// Badif( color === RED    || color === BLUE) {    // ....}

 

 

#Anonymous Functions (匿名函數)

Anonymous Functions通常出現在無需複用的函數中,可以有效控制變數範圍,構造閉包 (Closure),防止對全域變數造成汙染。

(function(){    // insert code here})();

在jquery等JavaScript庫中經常能見到。

 

#Module Pattern

http://www.yuiblog.com/blog/2007/06/12/module-pattern/

 

# 盡量把<script>放到Html內容的後面

無論是針對JavaScript檔案還是JavaScript語句,最好放到Html Body的最後部分;

 For performance reasons, script blocks can also be placed at the bottom of the document body

# Delayloading

 

聯繫我們

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