我在检测扩展,然后根据扩展名采取操作。所以我的问题是为什么不这样做,似乎够逻辑呃?Javascript if if statement with multiple or following condition
var ext = url.split('.').pop().toLowerCase();
if (ext == 'avi' || 'mpg' || 'mpeg' || 'mp4' || '3gp') {
This is a video (this always returns true...?)
} else if (ext == 'jpg' || 'jpeg' || 'gif' || 'png' || 'bmp') {
This is a picture
} else {
This extension isn't supported here
}
但这是吗?不必要的开销?
var ext = url.split('.').pop().toLowerCase();
if (ext == 'avi' || ext == 'mpg' || ext == 'mpeg' || ext == 'mp4') {
This is a video
} else if (ext == 'jpg' || ext == 'jpeg' || ext == 'gif' || ext == 'png') {
This is a picture
} else {
This extension isn't supported here
}
是否有语法问题,我的思念,使这项工作就像例子1没有遍地打变量?值得关注的是,因为这个列表比扩展的数量大很多,并且在完成所有的说明和完成时看起来像是大量不必要的代码。
'EXT =“JPG” || 'jpeg'||'等同于'(ext =='jpg')|| 'jpeg'||'等 – nhgrif
好问题.. – Hitesh