JAVASCRIPT基础学习篇(8)--ECMAScript Basic4(EcmaScript 表达式)
2009-02-27 14:47
381 查看
第四章 表达式
1、The if statement
if (condition) statement1 else statement2
The condition can be any expression条件可为任意一表达式,js会计算出结果,若为true则statement1...
if (condition1) statement1 else if (condition2) statement2 else statement3
2、Iterative statements迭代
do {
statement
} while (expression);
while(expression) statement
for (initialization; expression; post-loop-expression) statement
for (var i=0; i < iCount; i++){
alert(i);
}
for-in
The for-in statement is a strict iterative statement. It is used to enumerate the properties of an object.
Syntax:
for (property in expression) statement
For example:
for (sProp in window) {
alert(sProp);
}
Labeled statements
It is possible to label statements for later use with the following syntax:
label: statement
For example:
start: var iCount = 10;
The break and continue statements
Both the break and continue statements can be used in conjunction with labeled statements to return
to a particular location in the code. This is typically used when there are loops inside of loops, as in the
following example:
var iNum = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
break outermost;
}
iNum++;
}
}
alert(iNum); //outputs “55”
The with statement
The with statement is used to set the scope of the code within a particular object. Its syntax is the following:
with (expression) statement;
For example:
var sMessage = “hello world”;
with(sMessage) {
alert(toUpperCase()); //outputs “HELLO WORLD”
}
The switch statement
switch (expression) {
case value: statement
break;
case value: statement
break;
case value: statement
break;
...
case value: statement
break;
default: statement
}
Two big differences exist between the switch statement in ECMAScript and Java
In ECMAScript, the switch statement can be used on strings, and it can indicate case by nonconstant values:
在java中switch(expression)其中expression必须是int类型或能够转化为int类型的如char,bit,short int等,显然不能是字符串,而在js中是可以的,在c中也是可以的。
var BLUE = “blue”, RED = “red”, GREEN = “green”;
switch (sColor) {
case BLUE: alert(“Blue”);
break;
case RED: alert(“Red”);
break;
case GREEN: alert(“Green”);
break;
default: alert(“Other”);
}
1、The if statement
if (condition) statement1 else statement2
The condition can be any expression条件可为任意一表达式,js会计算出结果,若为true则statement1...
if (condition1) statement1 else if (condition2) statement2 else statement3
2、Iterative statements迭代
do {
statement
} while (expression);
while(expression) statement
for (initialization; expression; post-loop-expression) statement
for (var i=0; i < iCount; i++){
alert(i);
}
for-in
The for-in statement is a strict iterative statement. It is used to enumerate the properties of an object.
Syntax:
for (property in expression) statement
For example:
for (sProp in window) {
alert(sProp);
}
Labeled statements
It is possible to label statements for later use with the following syntax:
label: statement
For example:
start: var iCount = 10;
The break and continue statements
Both the break and continue statements can be used in conjunction with labeled statements to return
to a particular location in the code. This is typically used when there are loops inside of loops, as in the
following example:
var iNum = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
break outermost;
}
iNum++;
}
}
alert(iNum); //outputs “55”
The with statement
The with statement is used to set the scope of the code within a particular object. Its syntax is the following:
with (expression) statement;
For example:
var sMessage = “hello world”;
with(sMessage) {
alert(toUpperCase()); //outputs “HELLO WORLD”
}
The switch statement
switch (expression) {
case value: statement
break;
case value: statement
break;
case value: statement
break;
...
case value: statement
break;
default: statement
}
Two big differences exist between the switch statement in ECMAScript and Java
In ECMAScript, the switch statement can be used on strings, and it can indicate case by nonconstant values:
在java中switch(expression)其中expression必须是int类型或能够转化为int类型的如char,bit,short int等,显然不能是字符串,而在js中是可以的,在c中也是可以的。
var BLUE = “blue”, RED = “red”, GREEN = “green”;
switch (sColor) {
case BLUE: alert(“Blue”);
break;
case RED: alert(“Red”);
break;
case GREEN: alert(“Green”);
break;
default: alert(“Other”);
}
相关文章推荐
- JAVASCRIPT基础学习篇(5)--ECMAScript Basic1(EcmaScript 基本类型)
- JAVASCRIPT基础学习篇(4)--ECMAScript Basic:prototype属性及通过该属性建立面向对象的JavaScript
- JAVASCRIPT基础学习篇(6)--ECMAScript Basic2(EcmaScript 引用类型)
- JAVASCRIPT基础学习篇(10)--ECMAScript Basic6(Object Basics)
- JAVASCRIPT学习笔记--ECMAScript基础(1)
- JavaScript之基础-4 JavaScript 运算符、表达式
- javaScript基础(八)正则表达式
- JavaScript正则表达式基础知识汇总
- JavaScript 实现基础--正则表达式--简单模式--
- javascript正则表达式基础相关
- Javascript开发之五ECMAScript语法基础
- JavaScript正则表达式实例基础
- Web基础之认识JavaScript(LiveScript)、JScript和ECMAScript
- 从零开始学习前端JAVASCRIPT — 9、JavaScript基础RegExp(正则表达式)
- 【JavaScript】(8)JavaScript中的正则表达式创建方式、基础语法、及其compile | exec | test 方法
- javascript基础(正则表达式)(二十四)
- javascript学习(2)——[基础回顾]运算符、表达式、流程控制语句
- javaScript中的一些 正则表达式 的基础知识。
- 【JS】JavaScript高级程序设计笔记 第2章 ECMAScript基础
- JavaScript基础之数据类型转换 、运算符 &表达式