switch方法中使用数字区间

function getCategory(age) {    
    var category = "";    
    switch (true) {    
        case isNaN(age):    
            category = "not an age";    
            break;    
        case (age >= 50):    
            category = "Old";    
            break;    
        case (age <= 20):    
            category = "Baby";    
            break;    
        default:    
            category = "Young";    
            break;    
    };    
    return category;    
}    
getCategory(5);  // 将返回 "Baby"  

 

posted @ 2017-12-20 19:46  来亦何哀  阅读(3170)  评论(0编辑  收藏  举报