zoukankan      html  css  js  c++  java
  • Javascript学习笔记-基本概念-语句

    1、if语句

    if (condition) statement1 else statement2

    也可以像下面这样把整个if 语句写在一行代码中:

    if (condition1) statement1 else if (condition2) statement2 else statement3

    2、do-while语句

    do {
        statement
    } while (expression);

    后测试循环语句,在对条件表达式求值之前,循环体内的代码至少会被执行一次。

    3、while语句

    while(expression) statement

    前测试循环语句。

    4、for语句

    for (initialization; expression; post-loop-expression) statement

    由于ECMAScript 中不存在块级作用域,因此在循环内部定义的变量也可以在外部访问到。

    var count = 10;
    for (var i = 0; i < count; i++){
        alert(i);
    }
    alert(i); //10
    // 无限循环
    for (;;) { 
        doSomething();
    }
    
    
    //只给出控制表达式实际上就把for 循环转换成了while 循环
    var count = 10;
    var i = 0;
    for (; i < count; ){
        alert(i);
        i++;
    }

    5、for-in语句

    for-in 语句是一种精准的迭代语句,可以用来枚举对象的属性。

    for (property in expression) statement

    实例:

    for (var propName in window) {
        document.write(propName);
    }

    通过for-in 循环输出的属性名的顺序是不可预测的;

    如果表示要迭代的对象的变量值为null 或undefined,for-in 语句会抛出错误。建议在使用for-in 循环之前,先检测确认该对象的值不是null 或undefined。

    6、label语句

    使用label 语句可以在代码中添加标签,以便将来使用。

    label: statement
    start: for (var i=0; i < count; i++) {
        alert(i);
    }

     7、break和continue语句

    break 语句会立即退出循环,强制继续执行循环后面的语句。

    continue 语句虽然也是立即退出循环,但退出循环后会从循环的顶部继续执行。

    var num = 0;
    for (var i=1; i < 10; i++) {
        if (i % 5 == 0) {
            break;
        }
        num++;
    }
    alert(num); //4
    var num = 0;
    for (var i=1; i < 10; i++) {
        if (i % 5 == 0) {
            continue;
        }
        num++;
    }
    alert(num); //8    

     break 和continue 语句都可以与label 语句联合使用,从而返回代码中特定的位置。

    var num = 0;
    outermost:
    for (var i=0; i < 10; i++) {
        for (var j=0; j < 10; j++) {
            if (i == 5 && j == 5) {
                break outermost;
            }
            num++;
        }
    }
    alert(num); //55
    var num = 0;
    outermost:
    for (var i=0; i < 10; i++) {
        for (var j=0; j < 10; j++) {
            if (i == 5 && j == 5) {
                continue outermost;
            }
            num++;
        }
    }
    alert(num); //95

     8、with语句

    with 语句的作用是将代码的作用域设置到一个特定的对象中。

    with (expression) statement;

     定义with 语句的目的主要是为了简化多次编写同一个对象的工作,如下面的例子所示:

    var qs = location.search.substring(1);
    var hostName = location.hostname;
    var url = location.href;

    上面几行代码都包含location 对象。如果使用with 语句,可以把上面的代码改写成如下所示:

    with(location){
        var qs = search.substring(1);
        var hostName = hostname;
        var url = href;
    }

    大量使用with 语句会导致性能下降,同时也会给调试代码造成困难,因此在开发大型应用程序时,不建议使用with 语句。

    9、switch语句

    switch (expression) {
        case value: statement
            break;
        case value: statement
            break;
        case value: statement
            break;
        case value: statement
            break;
        default: statement
    } 

    特色:

    可以在switch 语句中使用任何数据类型(在很多其他语言中只能使用数值),无论是字符串,还是对象都没有问题。其次,每个case 的值不一定是常量,可以是变量,甚至是表达式。

    switch ("hello world") {
        case "hello" + " world":
            alert("Greeting was found.");
            break;
        case "goodbye":
            alert("Closing was found.");
            break;
        default:
            alert("Unexpected message was found.");
    }
    var num = 25;
    switch (true) {
        case num < 0:
            alert("Less than 0.");
            break;
        case num >= 0 && num <= 10:
            alert("Between 0 and 10.");
            break;
        case num > 10 && num <= 20:
            alert("Between 10 and 20.");
            break;
        default:
            alert("More than 20.");
    }

    switch 语句在比较值时使用的是全等操作符,因此不会发生类型转换(例如,字符串"10"不等于数值10)。

  • 相关阅读:
    python获取股票数据接口
    Excel使用VBA读取实时WebService股票数据
    安装Pycharm
    Pycharm2019使用
    KLine
    pycharm下用mysql
    新浪股票接口
    SpringBoot整合持久层技术--(二)MyBatis
    SpringBoot整合持久层技术--(一)JdbcTemplate
    SpringBoot整合WEB开发--(十)配置AOP
  • 原文地址:https://www.cnblogs.com/szwx1314/p/8151363.html
Copyright © 2011-2022 走看看