JavaScript Patterns and Good Parts

学习一种语言,自然要看看市面上的好书是一种捷径。这几天,我抽空看了看JavaScript Patterns和JavaScript good parts两本书,算是知识储备吧。

#ASI (Automatic Semicolon insert)

 JavaScript把自己搞得很只能,如果代码行末尾少了“;”,他还会择机给你加上一个。JavaScript Interpreter又不是神,时常会犯错误,所以通常我们不给他ASI爽一把的机会。

// Good
if (XX) {
    // ....
}

// Bad.
// Piaoger以前喜欢这种方式,看来得改改了,
// 争取写C/C++还有JavaScritpt时代码风格相似吧。
if (XX)
{
    // ....
}

//Good
 if( color === RED ||
    color === BLUE) {
    // ....
}


// Bad
if( 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

原文地址:https://www.cnblogs.com/piaoger/p/2491071.html