|| 或 语法 语法糖

一般情况 || 

function test(fruit) {
  if (fruit == 'apple' || fruit == 'strawberry') {
    console.log('red');
  }
}

但是有很多的或 || 那难道要写 很多 用 includes 函数 即可解决

function test(fruit) {
  // 条件提取到数组中
  const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];
 
  if (redFruits.includes(fruit)) {
    console.log('red');
  }
}

Array.includes代替Array.indexOf

posted @ 2018-10-30 11:08  xuanPhoto  阅读(177)  评论(0)    收藏  举报