js中的语句(二)

接着上一篇说一下以下四个语句 : 

  • for of 语句
  • break , continue 语句
  • with 语句
  • switch 语句

1. for of 语句

这个语句用来遍历可迭代的对象元素, 常见可迭代对象有数组,字符串 .

在使用 for of 语句来遍历对象时, 会调用对象中以[Symbol.iterator]为键的函数 , [Symbol.iterator]函数 返回一个迭代器对象, 并通过调用其 next() 方法陆续返回值

    const arr = [1, 2, 3, 4, 5];
    const str = 'abcde';
    for (const item of arr) {
      console.log(item); // 依次输出 1, 2, 3, 4, 5
    }
    for (const item of str) {
      console.log(item); // 依次输出 a, b, c, d, e
    }
    // 调用数组和字符串对象的[Symbol.iterator]函数 陆续返回值
    class Demo {
      constructor(start, end) {
        this.start = start;
        this.end = end;
      }
      *[Symbol.iterator]() {
        while (this.start < this.end) {
          yield this.start += 1
        }
      }
    };
    class Demo1 {
      constructor(start, end) {
        this.start = start;
        this.end = end;
      }
      [Symbol.iterator]() {
        return {
          start: this.start,
          end: this.end,
          next() {
            if (this.start <= this.end) {
              return { done: false, value: this.start++ };
            } else {
              return { done: true, value: this.start };
            }
          }
        }
      }
    };
    const example = new Demo(0, 5)
    for (const i of example) {
      console.log(i); // 依次输出1, 2, 3, 4, 5
    }
    const example2 = new Demo1(6, 10)
    for (const i of example2) {
      console.log(i); // 依次输出6, 7, 8, 9, 10
    }
    // 调用 example 和 example2 对象的[Symbol.iterator]函数 陆续返回值

2. break , continue 语句

这两个语句主要用于循环当中, 使用 break 会立即退出本次循环(循环结束), 执行循环体下一条语句 . continue 语句也会立即退出本次循环(循环尚未结束), 继续执行下一次循环

    // 在循环当中, 如果数组 arr 的长度大于3 , 就退出循环
    for (let i = 1; i < 5; i++) {
      if (arr.length >= 3) {
        break;
      }
      arr.push(i);
    }
    console.log(arr); // [1,2,3]

    let str = 'a';
    for (let i = 1; i < 7; i++) {
      if (str.split('')[3] === 'd') {
        str = str + String.fromCharCode(str.charCodeAt() + str.length + 1);
        continue;
      }
      str = str + String.fromCharCode(str.charCodeAt() + str.length);
    }
    // 在循环当中, 如果变量 str 分割成数组后的第四项是 'd' 的时候, 拼接之后第二位的字符
    // 所以最终结果是 'abcdfgh'

3. with 语句

with 语句就是将代码的作用域设置为特定的对象

使用 with 语句时,需要传入一个对象, with 的代码块内所访问的变量首先从参数对象的属性查找, 如果找不到则依据作用域链向外查找

    const obj = {
      name: 'zs',
      age: 18,
    }
    const sex = '男';
    with (obj) {
      console.log(name); // 'zs'
      console.log(age);  // 18
      console.log(sex);  // '男'
    }

4. switch 语句

switch 语句需要你传入一个表达式 (值, 变量...),  依次进行值的匹配 匹配到后 使用 break 代码块, 最好给每一个条件都加上 break, 避免不需要的匹配 .

switch 语句完全可以用 if 语句来实现 , 但是 switch 结构更加清晰

    const key = 'dog';
    switch (key) {
      case 'cat':
        console.log('我是一只猫');
        break;
      case 'dog':
        console.log('有一只狗');
        break;
      default:
        console.log('啥都没有');
        break;
    }

有问题大家可以再下面进行评论哈 ........

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值