我在檢測擴展,然後根據擴展名採取操作。所以我的問題是爲什麼不這樣做,似乎夠邏輯呃?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