# 《你所不知道的JS:導讀,型別與文法》讀書筆記 ## 行動 - [ ] 整理我自己的閱讀筆記 ## 構思 摘要與延伸心得(樹狀結構): --- ## 封存 讀冊taaze [購書連結](https://www.taaze.tw/usedList.html?oid=11100778444) 台北市圖書館[借閱連結](http://book.tpml.edu.tw/webpac/bookDetail.do?id=691394) ### 內文摘錄和我的附註: #### 導讀 * 沒有了解一個程式語言的來龍去脈會怎樣? * 無法寫出強大的程式碼 * 無法幫自己 debug * 作者教授: * web 相關標準知識 * semantic markup (語意標示碼) * 帶有良好註解的程式碼 * 作者對 Js 的態度 * 這是我們的技藝 * 我們不想要安於差勁的現狀 * 作者對讀者的期望 * 把習得技能前後的感覺放在心上 * 記住之所以開始閱讀這本書,是希望可以精進知識、踏上學習之旅 #### 第一章 程式設計簡介 * 程式碼 * statement(述句) * 例如: `a = b * 2;` * 變數 * a 和 b * literal value * 2 * 一個單純的值 * 運算子 * = 和 * 及其他很多 * 用分號 `;` 作為結尾 * ==expressions(運算式)== <br/>以`a = b * 2` 為例 * `2` 是 literal value expression * `b` 是 variable expression * `b * 2` 是 arithmetic expression * `a = b * 2` 是一個 assignment expression * ==expressions statement(運算式述句)== * 常見的如 **call expression** * 例如:`alert(a);` * 像是 `b * 2` 這種 expression statement 也可以獨立存在,但因為不能幹嘛所以很少見 * comment * `//`(單行註記) 或`/*` `*/`(多行註記) * 寫 why 不寫 what * 有時要寫 how #### 運算子分為以下幾類: - assign - `=` - math - `+`, `-`,`*`,`/`,`%` - 複合指定 - `+=`, `-=`, etc. - 遞增或遞減 - `++`, `--` - object properties 的存取 - `.` - console 是一個 object - 相等性 - `==`, `===`,`!=`,`!==` - 比較 - `<`, `>`,`<=`, `>=` - 邏輯 - `&&`and , `\\` or, `!`not #### value and type - number //42 - string //"42" - boolean // ture, false - array - function - object ##### Type coercion //參考[Type coercion](https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/) - explicit type coercion (明確的強制轉型) - 又叫做 type casting - 用內建函式轉型 coerce - 由寫程式的人要求執行轉型 - 如 `Number(value)` - implicit type coercion (隱含的強制轉型) - JavaScript 自己進行 coerce ##### 相等性 JS 的 4 個 equality operators: |符號|說明| |:---:|:---| |`==`|允許 implicit coercion 的相等性檢查| |`===`|不允許 implicit coercion 的相等性檢查| |`!=`|允許 implicit coercion 的不等性檢查| |`!==`|不允許 implicit coercion 的相等性檢查| [ES5 規格第11.9.3節](http://ecma-international.org/ecma-262/5.1/#sec-11.9.3)相關implicit coercion 轉換優先順序: // [ES5 規格第11.9.3節](http://ecma-international.org/ecma-262/5.1/#sec-11.9.3)相關implicit coercion 轉換優先順序 : **The Abstract Equality Comparison Algorithm** The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows: 1. If Type(x) is the same as Type(y), then 1. If Type(x) is Undefined, return true. 2. If Type(x) is Null, return true. 3. If Type(x) is Number, then 1. If x is NaN, return false. 2. If y is NaN, return false. 3. If x is the same Number value as y, return true. 4. If x is +0 and y is −0, return true. 5. If x is −0 and y is +0, return true. 6. Return false. 4. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false. 5. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. 6. Return true if x and y refer to the same object. Otherwise, return false. 2. If x is null and y is undefined, return true. 3. If x is undefined and y is null, return true. 4. If Type(x) is Number and Type(y) is String,<br/>return the result of the comparison x == ToNumber(y). 5. If Type(x) is String and Type(y) is Number,<br/>return the result of the comparison ToNumber(x) == y. 6. If Type(x) is Boolean,<br/>return the result of the comparison ToNumber(x) == y. 7. If Type(y) is Boolean, <br/>return the result of the comparison x == ToNumber(y). 8. If Type(x) is either String or Number and Type(y) is Object,<br/>return the result of the comparison x == ToPrimitive(y). 9. If Type(x) is Object and Type(y) is either String or Number,<br/>return the result of the comparison ToPrimitive(x) == y. 10. Return false. //[Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) - 直譯與編譯 - JavaScript 通常被認為是直譯式語言 - ==但 Javascript engine 其實是經過即時編譯的語言== ## 內建函式 |符號|說明| |:---:|:---| |.toFixed(number)|指定要取到小數點後幾位,==並產生相應的string==| | ## block區塊 - 用a curly-brace pair(一對大括號`{}`)隔開 - 裡面有一句或多句 statement - 一個 block 不需要 `;` 做結尾 ## loop - loop 包含 a test condition, 和 a block - loop block 每次執行時,就成為一次 iteration(迭代) ### do while syntax: ```javascript= do{ statement; } while (conditional) ``` ### while ```javascript= while (conditional){ statement; } ``` - do..while 和 while 差別在於 - while 會先做 conditional test 才進入第一次 iteration - do..while 則會先執行第一次 iteration 才會進行 conditional test ### for ```javascript= for(conditional){ statement; } ``` 例如: ```javascript= for(var i = 0; i <= 9; i ++){ console.log(i); } //0 1 2 3 4 5 6 7 8 9 ``` ## function 函式 - 可重複使用的一個具名片段 - 也可以用來將相關的程式碼組織成具名的集合 - 可以藉由名稱來 call function ## scope - lexical scope - 只有在 function 內的 code 可以存取 scoped (範疇內的)varible ### nested 內嵌 - 一個 scope 可以 nested 在另一個 scope 裡面 - 裡面的 code 可以存取外面的 varible,但外面的 code 不可以存取裡面的 varible #### 回想筆記 20190528 Morning - const - 常數 - 使用常數可以在發生任何改動時只需要修正該 const // 目前為止覺得應該是很適合新手直接掃過去,然後把東西做成索引目錄,在初期階段時回來看的一本書。 回想筆記0529 Morning - 區塊 block - 條件式 - if - truthy - falsy - 迴圈 - do while - while - for - 函式 - scope - nested #### 第一章 程式設計簡介 ###### tags: `JavaScript` `基本運算` `邏輯運算` `機能紹介` `功能` `syntax` `type` `const`