美文网首页
代码优化篇

代码优化篇

作者: 曼少女 | 来源:发表于2019-01-30 10:29 被阅读0次

多重判断使用Array.includes

以下判断,正常写法:

if (animal == 'cat' || animal== 'dog' || animal== 'pig' || animal== 'bat') { // ...if the value are too much, I'll be headache
    console.log('Circle of friends like zoo');
  }

使用Array.includes优化

function (who) {
var arr = ['cat', 'dog', 'bat', 'pig'];
if (arr.includes(who)) {
    console.log('Circle of friends like zoo');
 }
}
console.log(arr.includes('cat'));  // expected output: true
console.log(arr.includes('at'));  // expected output: false

相关文章

网友评论

      本文标题:代码优化篇

      本文链接:https://www.haomeiwen.com/subject/kmuqsqtx.html