是否有更短的方法来处理多个 if else 条件?
if( suffix != 'jpg' && suffix != 'jpeg' && suffix != 'png' && suffix != 'gif'){
console.log('not an image.');
}
最佳答案
使用数组可以看作是一种速记,尽管它确实增加了(恕我直言,可以忽略不计)开销:
if (['jpg', 'jpeg', 'png', 'gif'].indexOf(suffix) === -1) {
console.log('not an image.');
}
编辑:使用 RegExp 更短:
if (!/jpe?g|png|gif/.test(suffix)) {
console.log('not an image.');
}
关于javascript - 使用 JS 是否有多个 if else 条件的简写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22136170/