zoukankan      html  css  js  c++  java
  • Javascript高级之流控制语句

    流控制语句

    if 语句

    • 语法
      • if(condition) statement1 else statement2

    do-while 语句

    • 语法
    do{
      statement
    }while(expression);
    

    while 语句

    • 语法
      • while(expression) statement

    for 语句

    • 语法

      • for(初始化; 条件表达式; 循环后表达式) statement
    • 概念

      • 使用let声明迭代器变量
      • 初始化,条件表达式,循环后表达式都不是必需的
      • 如果只包含条件表达式,那么for循环就变成了while循环
    // 无穷循环
    for(;;){
      doSomething();
    }
    
    // while循环
    for(;条件表达式;){
      doSomething();
    }
    

    for-in 语句

    • 语法
      • for(property in expression) statement
    // 循环显示BOM对象window的所有属性
    for(const proname in window){
        console.log(proname);
    }
    
    • 概念
      • for-in语句是一种严格的迭代语句,用于枚举对象中的非符号键属性
      • for-in语句返回对象属性的顺序因浏览器而异
      • 如果for-in循环要迭代的变量是null或undefined,则不执行循环体

    for-of 语句

    • 语法
      • for(property of expression) statement
    for(const el of [2, 4, 6, 8, 10]){
        console.log(el);
    }
    
    • 概念
      • for-of语句是一种严格的迭代语句,用于遍历可迭代对象的元素
      • for-of循环会按照可迭代对象的next()方法产生值的顺序迭代元素
      • 如果尝试迭代的变量不支持迭代,则for-of语句会抛出错误

    标签语句

    • 语法

      • label:statement
    • 概念

      • 标签语句用于给语句加标签
      • 标签语句的典型应用场景是嵌套循环

    break 和 continue 语句

    • 概念
      • break 和 continue 语句为执行循环代码提供了更严格的控制手段
      • break语句用于立即退出循环,强制执行循环后的下一条语句
      • continue语句用于立即退出循环,但会再次从循环顶部开始执行
      • break和continue都可以与标签语句一起使用,返回代码中特定的位置
    let num= 0;
    outermost:
    for(let i=0; i<10; i++){
        for(let j=0; j<10; j++){
            if(i==5&&j==5){
                break;
            }
            num++;
        }
    }
    console.log(num);   // 95
    
    let num= 0;
    outermost:
    for(let i=0; i<10; i++){
        for(let j=0; j<10; j++){
            if(i==5&&j==5){
                break outermost;
            }
            num++;
        }
    }
    console.log(num);   // 55
    
    let num= 0;
    outermost:
    for(let i=0; i<10; i++){
        for(let j=0; j<10; j++){
            if(i==5&&j==5){
              continue;
            }
            num++;
        }
    }
    console.log(num);   // 99
    
    let num= 0;
    outermost:
    for(let i=0; i<10; i++){
        for(let j=0; j<10; j++){
            if(i==5&&j==5){
              continue outermost;
            }
            num++;
        }
    }
    console.log(num);   // 95
    

    with 语句

    • 语法

      • with (expression) statement;
    • 概念

      • with语句的用途是将代码作用域设置为特定的对象
      • 使用with语句的主要场景是针对一个对象反复操作
      • 不推荐使用
    let qs= location.search.substring(1);
    let hostName= location.hostname;
    let url= location.href;
    // 等价于
    with(location){
      let qs= search.substring(1);
      let hostName= hostname;
      let url= href;
    }
    

    switch 语句

    • 语法
    switch(expression){
      case value1:
        statement
        break;
      case value2:
        statement
        break;
      case value3:
        statement
        break;
      default:
        statement
    }
    
    • 概念
      • switch语句可以用于所有数据类型
      • 条件的值不需要是常量,也可以是变量和表达式
    switch('你好 中国'){
      case '你好'+' 中国':
        console.log('success');
        break;
      default:
        console.log('fail');
    }
    
    let num= 25;
    switch(true){
      case num<0:
        console.log('小于0');
        break;
      case num>0:
        console.log('大于0');
        break;
      default:
        console.log('等于0');
    }
    
  • 相关阅读:
    class线程信息
    Class 文件简介
    JVM对象及垃圾回收处理
    jvm体系结构
    查找

    二叉树
    队列


  • 原文地址:https://www.cnblogs.com/SharkJiao/p/14157523.html
Copyright © 2011-2022 走看看