2014年10月28日 星期二

20141028 今日學習

今天讀了一篇好文章,我太喜歡下面這句話了,所以摘錄下來
看到一張有趣的圖


讀Eloquent JavaScript的CH2
對於expression,Eloquent JavaScript這本書的作者有著下面美好的描述
  • A fragment of code that produces a value is called an expression. Every value that is written literally (such as 22 or "psychoanalysis") is an expression. An expression between parentheses is also an expression, as is a binary operator applied to two expressions or a unary operator applied to one.
對於expression與statement的差異,也有著很好的解釋
  • If an expression corresponds to a sentence fragment, a JavaScript statement corresponds to a full sentence in a human language. A program is simply a list of statements.
提到expresson後面可以不加分號 " ; "
  • In some cases, JavaScript allows you to omit the semicolon at the end of a statement. In other cases, it has to be there, or the next line will be treated as part of the same statement. The rules for when it can be safely omitted are somewhat complex and error-prone. 
喔喔喔,有趣了,我原本以為是可以都不用加分號,原來是有選擇性的,所以我們再來估狗讀讀其他資料吧

讀到的第一篇資料的第一個範例就很正
在瀏覽另一篇時,讀到一句話,想到threex.cubecamera-master\examples\demmo.html在實作匿名函數時有用到
這篇前面其實我也沒認真讀,但是一定要看最下面那個範例,講到JavaScript裡semicolon與parentheses在進行interpreting時容易搞混的地方
  • when a line starts with parenthesis, prepend a semicolon to it.

OK,學習到這邊對expression與statement稍微有點概念了,根據昨天的紀錄,現在讓我們回去學習Functions :: Eloquent JavaScript的Declaration notation這一節吧

Declaration notation這節除了解釋了為何可以先調用function的名字,後面再定義function,也說明了在if時不適合用function
  • What happens when you put such a function definition inside a conditional (if) block or a loop? Well, don’t do that. Different JavaScript platforms in different browsers have traditionally done different things in that situation, and the latest standard actually forbids it.
現在,CH2 - The call stack這節

其實就只是在說你function調用時會放在stack的頂端,用完後再把它從stack中移除
  • The place where the computer stores this context is the call stack. Every time a function is called, the current context is put on top of this “stack”. When the function returns, it removes the top context from the stack and uses it to continue execution.
現在,CH2 - Optional Arguments

這段期時就在說JavaScript心胸寬大,你多傳argument進來,他也不會報錯,只會取原本function定義的,換句話說:在argument的處理自己要小心,complier不會幫你debug
  • JavaScript is extremely broad-minded about the number of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters simply get assigned the value undefined.
  • The downside of this is that it is possible—likely, even—that you’ll accidentally pass the wrong number of arguments to functions and no one will tell you about it.
現在,CH2 - Closure

這篇提到Closure閉包,在先前(20141017 今日學習)曾花一天時間的時間學習之,現在來讀讀跟先前學習的有什麼差別,也許會有不同的領悟

稍微讀了一下後,恩.......很好,我過去把Closure與匿名函數搞混了,今天似乎有必要把Closure、module pattern重新釐清才對

找到這篇,對於Closure的描述清楚多了
簡單來說就是一個function裡面再包一個function,而裡面的function可以調用外面function的variable以及parameter
  • A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.
這裡又說到Closure另一個有趣的地方,內部的function可以調用外部function的parameter,但如果你外部的function放入一個argument object,內部的function無法調用外部的這一個argument object
  • The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.
喔喔喔,這篇提到Closure廣泛用於Node.js與jQuery,另外這篇也討論了Closure與side effect的關聯,對side effect這詞有印象,所以繼續讀下去
  • Closures have access to the outer function’s variable even after the outer function returns

讀完了這篇對於Closure與side effect的關聯,讓我想起曾在Functions :: Eloquent JavaScript這章Closure的那節讀到的multiplier()這個範例,有讀到相同的概念,現在以multiplier()來解釋,以下是我自己的理解

一開始declare twice時,variable twice be reference to a function mutliper(2),在一開始的時候twice就有parameter = 2 放入multiplier()的factor裡
而我們的twice如code所示,是由一個function定義的,twice它的本質是function,所以我們可以再給twice一個parameter
由於一開始 var twice時,outer function的parameter已經給了2,所以再放個5到twice的parameter時,5只能放到inner function的parameter去

Understand JavaScript Closures With Ease這篇還有提到Closure的其他用法,像是用在array或是Closures store references to the outer function’s variables,至於這些等以後有用到時再讀吧

另外還有一篇對Closure做了精美的解釋,不紀錄對不起自己啊
稍微瀏覽一下,這篇似乎講到JavaScript更底層的東西,甚至出現以前未曾讀過的LexicalEnvironment object,瀏覽僅知 var a=5; 這樣一條expression就是根據LexicalEnvironment object去寫的,這篇講得又更深入了,改天有空再讀吧。

OK,現在回頭去讀Functions :: Eloquent JavaScript的Closure那節,應該更容易理解。讀後感想,這節的解釋手法其實還好 XD 並不好讀,不過我已經從其他資源懂了Closure,所以沒差。

現在,CH2 - Recursion

喔喔,有趣了,上週末回去前,對Recusion有更深一層體悟,明天(10/29)來做個紀錄吧。

現在想換個口味讀,所以來讀Higher-Order Functions :: Eloquent JavaScript

讀前面幾句感想,程式短小精幹且優美很重要,厚重肥大的程式碼很解

Higher order function,其實就是在說一個function的parameter放另一個function,這篇開頭的範例以兩種加總的寫法來說明為何要寫Higher order function,其實是因為定義明確,以加總為例,最簡單的寫法寫個while loop或是for loop即可,行數又短,但是它可能遭遇的問題是定義不明確,其他人需要去猜你的code在幹嘛,這不是一個elegant coding style in JavaScript

為了讓我們能更清楚每段程式碼在幹嘛,我們在為每個function命名時要考慮,這個名字是否是個恰當的abstraction,這樣才夠清楚
  •  Abstractions hide details and give us the ability to talk about problems at a higher (or more abstract) level.

有點倦怠了,在此備份明天要讀的資料,先學完higher order function,再回去複習module patern,最後回去讀recursion

10/29 higher order function學習教材備份

沒有留言:

張貼留言