expression (運算式) | An expression is a phrase of JavaScript that a JavaScript interpreter can evaluate to produce a value | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Primary Expressions |
|
||||||||||||||||||||||||||||
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 (運算子) |
|
||||||||||||||||||||||||||||
Arithmetic Expressions |
|
||||||||||||||||||||||||||||
Relational Expressions |
|
||||||||||||||||||||||||||||
Logical Expressions |
|
||||||||||||||||||||||||||||
Assignment Expressions |
|
||||||||||||||||||||||||||||
Evaluation Expressions |
|
||||||||||||||||||||||||||||
Miscellaneous Operators 其他各種的運算子 |
|
||||||||||||||||||||||||||||
Operation predec |
![]() |
它是 JavaScript 的 句子 (sentences) 或 命令 (commands) .
** Expressions are evaluated to produce a value
but statements are executed to make something happen.
|
|||||||||||||
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 宣告述句 |
|
||||||||||||
Conditionals |
|
||||||||||||
Loops |
|
||||||||||||
Jumps |
|
||||||||||||
Miscellaneous Statements |
|