運算式與運算子
expression (運算式) An expression is a phrase of JavaScript that a JavaScript interpreter can evaluate to produce a value
Primary Expressions
  • constant (常數)
  • literal values (字面值)
  • language keywords
  • variable references
Object and Array Initializers
物件和陣列的初始設定式
    var a = [];
    var b = [1 + 2, 3 + 4];
    var matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
    var sparseArray = [1, , , , 5];

    var p = { x: 2.3, y: -1.2 };    // An object with 2 properties
    var q = {};                     // An empty object with no properties 不是 null 也不是 undefined
    
    q.x = 2.3;
    q.y = -1.2; // Now q has the same properties as p

    var rectangle = {
        upperLeft: { x: 2, y: 2 },
        lowerRight: { x: 4, y: 5 }
    };

    var side = 1;
    var square = {
        "upperLeft": { x: p.x, y: p.y },
        'lowerRight': { x: p.x + side, y: p.y + side }
    };
These initializer expressions are sometimes called "object literals" and "array literals."
這些初始設定式有時被稱為物件字面值、物件字面值
Function Definition Expressions
函式定義運算式
也叫做函式字面值 (function literal)
    var square = function (x) {
        return x * x;
    };

Property Access Expressions
屬性存取運算式
expression.identifier
expression[expression]
如果 obj.property 的值是null或undefined , 該運算式會丟出 TypeError . .identifier 語法較簡單直覺,但是如果 .indntifier 是保留字或是數字...那就得改用中括號來處理.
Invocation Expressions
調用運算式
    f(0)                // f is the function expression; 0 is the argument expression.
    
    Math.max(x,y,z)     // Math.max is the function; x, y and z are the arguments.
    
    a.sort()            // a.sort is the function; there are no arguments.
上方的 a.sort() 稱為 method invocation (方法調用)
Object Creation Expressions
物件創建運算式
    var x = new Object();

    var p = new Point(2,3);
Operator (運算子)
Number of Operands
Operand and Result Type
L values
Operator Side Effects
Operator Precedence
Operator Associativity
Order of Evaluation
Arithmetic Expressions
The + Operator
Unary Arithmetic Operators
Bitwise Operators
Relational Expressions
Equality and Inequality Operators
Comparison Operators
The in Operator
The instanceof Operator
Logical Expressions
Logical AND (&&)
邏輯 AND
  1. 用於 boolean 運算元之時 , 第一個及第二個皆為 true 才會回傳 true . if (x == 0 && y == 1) { }
  2. 用於關係運算式的連接詞, k = x == 0 && y == 0;
  3.     //1. 左邊是 falsy , 整個運算式為 falsy 所以 && 就直接回傳左邊的值
        //2. 左邊是 truthy , 再估算右邊的值 為 truthy 回傳右邊的值
        //                                為 falsy  也是回傳右邊的值
    
        //重點就是要記得 falsy 有哪些值 : false、null、undefined、0、-0、NaN、''
    
        var o = { x: 1 };
        var p = null;
        var a = o && o.x;       // => 1 , o 為 truthy 所以回傳 o.x
        var aa = o && o.k;      // => undefined , o 為 truthy o.k 為 undefined
        var b = p && p.x;       // => null , p 為 falsy 所以直接回傳 p
    

Logical OR (||)
Logical NOT (!)
The instanceof Operator
Assignment Expressions
Assignment with Operation
Logical OR (||)
Logical NOT (!)
The instanceof Operator
Evaluation Expressions
eval() 如果參數不是字串, 它會回傳原型
Global eval()
Strict eval()
Miscellaneous Operators
其他各種的運算子
The Conditional Operator (?:)
typeof ( )
傳入參數型別回傳值 (字串)
undefined"undefined"
null"object"
true、false"boolean"
任何數字或 NaN"number"
任何字串"string"
任何函式"function"
any nonfunction native object"object"
any host objectAn implementation-defined string, but not “undefined”, “boolean”, “number”, or “string”.
對所有物件及陣列 (function除外), typof 都回傳 'object' . 為了分辨物件的類別, 還是得使用其他像是 instanceof , class attribute , constructor 特性來區別
delete 作用於刪除物件屬性或陣列元素 , 刪除後的屬性是不存在的 (所以存取時會回傳 undefined) , 刪除陣列元素後會留下一個空位 (不會改變Array.length).
void

                                
The Comma Operator (,)
Operation predec Operation prededence
Statements 述句
它是 JavaScript 的 句子 (sentences) 或 命令 (commands) .

** Expressions are evaluated to produce a value
but statements are executed to make something happen.
  • Conditionals (if, switch)
  • Loops (for...for each...while)
  • Jumps (break, return, throw)
Expression Statements
Compound and Empty Statements
                        for(i = 0; i < a.length; a[i++] = 0) ;      //將 a 陣列的元素皆初始化為 0
                    
                        if ((a == 0) || (b == 0));  // 小心這個分號, 所以還是要用 { } 比較不易失誤
                        o = null;                   // 這一行因為分號標錯所以永遠會執行
                    
Declaration Statements
宣告述句
var
var i;                                          // One simple variable
var j = 0;                                      // One var, one value
var p, q;                                       // Two variables
var greeting = "hello" + name;                  // A complex initializer
var x = 2.34, y = Math.cos(0.75), r, theta;     // Many variables
var x = 2, y = x*x;                             // Second var uses the first
var x = 2,                                      // Multiple variables...
f = function(x) { return x*x }, y = f(x);       // each on its own line
                        
function
函式宣告述句

function funcname([arg1 [, arg2 [..., argn]]]) {
    statements....
}


var factor = function(x) { return x+1; }     // 函式定義運算式
function factorialFun(x) { return x+1; }     // 函式宣告述句 

兩者都會創建新的函式物件, 差別在後面那個會將函式名稱 factorialFun 宣告為變數並把新創的物件指定給該變數.
與 var 變數一樣的 hoisted 效果.
只是 factor 的程式碼還在原位, factorialFun及其程式碼都被hoisted了. (不懂這有什麼實際上的差異??)...
function declaration statements 可以 nested 在其他function中,但是最好放在最上層, 而且不要放在if, while迴圈或其他述句中 ( is nonportable ).
Conditionals
if
else if
switch
case 匹配是 === (同一性) 相等, 而不是 == (相等性運算子)....
Loops
while
do/while
for
    // 三個運算式都可以省略 (但分號不能省略)
    for(initialize ; test ; increment) {
        statement
    }

    //與下列 while 廻圈的表示法相同
    initialize;
    while(test) {
        statement
        increment;
    }
    // 多個變數隨迴圈迭代
    var i,j;
    for(i = 0, j = 10 ; i < 10 ; i++, j--) {
        sum += i * j;
    }

    // Return the tail of linked list o
    function tail(o) { 
        //省略了 initialize , 只要 test 為 truthy 即會執行 statement
        for(; o.next; o = o.next)   
            statement
        }
        
        return o;
    }
for/in
for in 只會針對 enumerable 的屬性列出, 但並不是所有的 property 都是可列舉的 , 許多內建物件的屬性是 nonenumerable 的, 不過自己定義的都是可以列舉的.
    
    // o 為 undefined 或 null 即會跳出該迴圈 , 
    //它會針對 o 下面每個 enumerable 的屬性都跑一次
    
    for (var p in o) {           // p 是屬性名稱
        if (o.hasOwnProperty(p)) {
            document.write(p + o[p])
        }
        console.log(o[p]);      // 印出屬性值
    }
Jumps
Labeled Statements
只要加上 : 任何 statement 都可以被標記 還是少用的好
    identifier: statement
break
continue
return
throw
如果不用在try cache裡...throw 是沒什麼意義的...因為大部份的瀏灠器都會忽略它
try/catch/finally
  1. catch 的 error 參數具有 block scope 與一般變數不一樣, 它只在 catch 區塊中有定義
  2. try 裡面使用 break, return 時, 仍然會執行完 finally 的 statement 之後再跳開
  3. finally 區塊裡發生例外或有 break, return 等跳開的程序時, 會先以finally的為優先來處理.


AA
Miscellaneous Statements
with
with 還是少用 , 在 strict 模式中會被禁用
with(document.forms[0]) {

    name.value = "";
    address.value = "";
    email.value = "";
}
debugger
'use strict'
它是指示令 (directive) 與一般 statement 不同, 差異如下
  1. 它不包含任何語言關鍵字, 沒有實作ECMAScript 5 的是沒作用的, 不會採取任何動作.
  2. 只出現在.js或函式主體的開頭, 如果出現在函式主體內的話, 就只有該函式為 strict 模式


與一般正常的差異在於
  1. ** 不允許 with 述句
  2. ** 變數必須宣告
  3. ** 函式(非方法), 它的 this 值為 undefined (一般是全域物件)
  4. nonwritable 屬性的 assign, 或是在 nonextensible 物件上建立新的屬性都會產生TypeError (一般會錯,但不會告知)
  5. 傳給eval()的程式碼不能在呼叫者的scope中宣告變數或定義函式
  6. arguments會有一份靜態複件 (static copy). ( function 下的Arguments )
  7. delete 會產生 SyntaxError 如果後面是 unqualified identifier such as a variable, function, or function parameter.
  8. delete nonconfigurable 屬性會產生 TypeError
  9. 在物件定義同樣名稱的 property 會出現語法錯誤
  10. 定義同樣名稱的 function (參數也相同) 會出現語法錯誤
  11. 八進位整數字面值 (0開頭) 是不被允許的
  12. eval 與 arguments 被視為關鍵字, 無法變更、指定
  13. call stack 能力是受限的 . arguments.caller 與 arguments.callee 都會出現 TypeError