JavaScript 两个最重要的概念表达式和语句
表达式概念:An expression is a phrase of JavaScript that a JavaScript interpreter can evaluate to produce a a value.
表达式的分类:
1.Primary Expressions
1 "hi word" 2 /pattern/ 3 4 //some of JavaScript reserved word are primary expressions 5 true 6 false 7 null 8 this //Evaluates to the "current" object 9 10 //finally the third type of primary expressions is the bare variable reference 11 I 12 sum 13 undefined // undefined is a global variable ,not like null.
2.Object and Array Initalizers
1 // Array initializer 2 [1,5,2,6,5] 3 4 // Object initializer 5 {x:2,y3}
3. Function Definition Expressions
1 //*function expression 2 function(x) { return x*x } 3 4 //* function statement 5 function foo(){ 6 console.log("bar"); 7 };
4.Property Access expression & Invoication expression & Object Creation expression
1 //Property Access expression 2 var o={x:1,y:{z:3}} 3 var a=[o,1,4,[5,6]] 4 5 o.x //property x of expression o 6 o["y"] //property x of expression o 7 a[1] //element at index 1 of object o 8 a[0].x // property x of expression a[0] 9 10 //Invocation expression 11 foo(1,2) 12 a.sort() 13 Math.max(x,y,z) 14 15 //object creation expression 16 new Object() 17 new Point(2,3)
5.Arithmetic Expression & Logical Expression & Assignment Expression
1 //Arithmetic 2 1+1 3 2*5 4 3-4 5 6%9 6 5-- 7 6++ 8 9 //Logical 10 7&8 11 7<<2 12 ^0x0f 13 // Assignment 14 I=6 15 o.x=2 16 17 x +=x+1 18 .......
语句概念:statements are JavaScript sentences or commands;Expressions are evaluated to produce a value, but statements are
executed to make something happen.
语句的分类:
Statement | Syntax | Purpose |
break | break [label]; | Exit from the innermost loop or switch or from named enclosing statement |
case | case expression: | Label a statement within a switch |
continue | continue [label]; | Begin next iteration of the innermost loop or the named loop |
debugger | debugger; | Debugger breakpoint |
default | default: | Label the default statement within a switch |
do/while | do statement while (expression); | An alternative to the while loop |
empty ; | Do nothing | |
for | for(init; test; incr) statement | An easy-to-use loop |
for/in for | (var in object) statement | Enumerate the properties of object |
function | function name([param[,...]]) { body } | Declare a function named name |
if/else | if (expr) statement1 [else statement2] | Execute statement1 or statement2 |
label | label: statement | Give statement the name label |
return | return [expression]; | Return a value from a function |
switch | switch (expression) { statements } | Multiway branch to case or default: labels |
throw | throw expression; | Throw an exception |
try | try { statements } | Handle exceptions |
[catch { handler statements }] | ||
[finally{ cleanup statements }] | ||
use strict | use strict; | Apply strict mode restrictions to script or function |
var | var name [ = expr] [ ,... ]; | Declare and initialize one or more variables |
while | while (expression) statement | A basic loop construct |
with | with (object) statement | Extend the scope chain (forbidden in strict mode) |
明确了概念后,我们需要非常清晰的分出来JavaScript的表达式和语句; 在后面的学习中你会发现表达实在是太重要了,它的作用域特殊,它的执行特殊;
1 /*确认下面code到底是什么?*/ 2 var a="123"; //? 3 var a=1,b=2;//? 4 var a,b//? 5 6 var f=function(x){ return x+1; } //? 7 function() {} //? 8 function foo(){}//? 9 10 i++;//? 11 a+b;//? 12 (x+y);//? 13 14 {x:2}//? 15 var a={x:2}//? 16 a.x//? 17 18 /*block*/ 19 while(x<10){ 20 x++; 21 } //? 22 23 var x=1; 24 { 25 var x=2; 26 } //? 27 28 if(x>1){} //?