JAVASCRIPT基础学习篇(8)--ECMAScript Basic4(EcmaScript 表达式)

 第四章 表达式


    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”);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值