js statement(js 语句)

switch 语句

 switch(typeof x) {
      case 'number':            // Convert the number to a hexadecimal integer
        return x.toString(16);
      case 'string':            // Return the string enclosed in quotes
        return '"' + x + '"';
      case 'boolean':           // Convert to TRUE or FALSE, in uppercase
        return x.toString( ).toUpperCase( );
      default:                  // Convert any other type in the usual way
        return x.toString( )
    }
case 关键字后跟随的一般是 数字和字符串常量,也可以跟表达式,但是会产生不可思议的副作用,所以不提倡用expression。

 

for/in 语句

语法如下:

for (variable in object)
    statement

variable should be either the name of a variable(应该是一个变量名), a var statement declaring a variable, an element of an array, or a property of an object

object is the name of an object or an expression that evaluates to an object 。

The for/in statement provides a way to loop through the properties of an object. The body of the for/in loop is executed once for each property of object. Before the body of the loop is executed, the name of one of the object's properties is assigned to variable, as a string. Within the body of the loop, you can use this variable to look up the value of the object's property with the [] operator

for (var prop in my_object) {
    document.write("name: " + prop + "; value: " + my_object[prop], "<br>");
}

 

Note that the variable in the for/in loop may be an arbitrary expression, as long as it evaluates to something suitable for the left side of an assignment. This expression is evaluated each time through the loop, which means that it may evaluate differently each time. For example, you can use code like the following to copy the names of all object properties into an array:(通过下面的代码,把一个对象的所有属性名复制到一个数组中)

 

var o = {x:1, y:2, z:3};
var a = new Array( );
var i = 0;
for(a[i++] in o) /* empty loop body */;

 

注意 对象引用属性的方法 既可以通过 o.x,也可以通过这种方式:o["x"]

 

break 语句

 

break 语句会使程序立即跳出最内层的循环或switch语句,语法为 break;

break 后还可以跟一个标签名: break labelname;

对break 语句中的标签中的唯一限制就是他命名的是一个封闭的语句。

 

outerloop:
  for(var i = 0; i < 10; i++) {
    innerloop:
      for(var j = 0; j < 10; j++) {
          if (j > 3) break;             // Quit the innermost loop
          if (i == 2) break innerloop;  // Do the same thing
          if (i == 4) break outerloop;  // Quit the outer loop
          document.write("i = " + i + " j = " + j + "<br>");
      }
  }
  document.write("FINAL i = " + i + " j = " + j + "<br>");

 

或者:

 test:{

       var a = 2 ;

        ...

        if(a =5){

           break test;

     }

}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值