踩坑笔记

52 阅读1分钟

结构赋值默认值问题

ES6 内部使用严格相等运算符===,判断一个位置是否有值。所以,只有当一个数组成员严格等于undefined,默认值才会生效。

let [x=1]=[undefined];// 1
let [x=1]=[null];    //null

forEach无法跳出循环

除抛出异常之外,没有其他方法可以停止或中断循环。如果您需要这种行为,则该forEach()方法是错误的工具。 在forEach中 执行return 是无法结束整个方法的 return只是跳出来整个循环 类似break

let arr=[1,2,3]
  arr.forEach(item=>{
    if(item===2){
      console.log('进入if')
      return 
      console.log('lalala')
    }
    console.log('hello',item)
  })
  console.log('方法结束')
  hello 1
  进入if
  hello 3
  方法结束