有没有更有效的方法来编写这个函数?

时间:2011-01-26 02:33:00

标签: javascript jquery

function month(num) {
    if (num == 1) {
        return "January";
    } else if (num == 2) {
        return "Feburary";
    } else if (num == 3) {
        return "March";
    } else if (num == 4) {
        return "April";
    } else if (num == 5) {
        return "May";
    } else if (num == 6) {
        return "June";
    } else if (num == 7) {
        return "July";
    } else if (num == 8) {
        return "August";
    } else if (num == 9) {
        return "September";
    } else if (num == 10) {
        return "October";
    } else if (num == 11) {
        return "November";
    } else if (num == 12) {
        return "December";
    } else {
        return false;
    }
}

jQuery的/使用Javascript。

6 个答案:

答案 0 :(得分:12)

是的,使用月号作为字符串数组(月份名称)的索引。

答案 1 :(得分:4)

function month(num) {
    if (num < 1 || num > 12 ) { return false; }
    var months = ["January","Feburary","March","April","May","June","July","August","September","October","November","December"];

    return months[num-1]

}

答案 2 :(得分:4)

或者,你可以采用错误的方式:

function month(num) {
    return new Date(0,num-1).toLocaleDateString().split(" ")[1];
}

看看它有多短! :)

唉,很有可能在各种浏览器和国家/地区中断。或者,它可能会自动为您翻译月份名称。

无论如何,不​​要这样做。

欢迎你。

答案 3 :(得分:3)

var month = function(n){
    return ["January", "February", "March",
         "April", "May", "June", "July",
         "August", "September", "October",
         "November", "December"][n-1]||false;
};
alert(month(3));

以下是使用模块模式进行操作的示例(因为这就是所有酷孩子最近所做的事情):

var MonthModule = (function(){
    var MonthsArray = [
        "January", "February", "March",
        "April", "May", "June", "July",
        "August", "September", "October",
        "November", "December"    
    ];
    var MonthsEnum = {
        "January" : 1, "February" : 2, "March" : 3,
        "April" : 4, "May" : 5, "June" : 6,
        "July" : 7, "August" : 8, "September" : 9,
        "October" : 10, "November" : 11, "December" : 12
    };

    var getMonthFromNumber = function(n){
        return MonthsArray[n-1]||false;
    };

    var getMonthFromName = function(s){
        return MonthsEnum[s]||false;
    };

    return {
        getMonthFromNumber : getMonthFromNumber,
        getMonthFromName : getMonthFromName
    };
}());

alert(MonthModule.getMonthFromNumber(5));
alert(MonthModule.getMonthFromName("February"));

答案 4 :(得分:1)

我有一段时间没有触及Javascript,所以语法可能很糟糕,但我很确定这应该可行。

my.namespace.MonthEnum = {
    JANUARY   : { value:  1, name: "January" },
    FEBRUARY  : { value:  2, name: "February" },
    MARCH     : { value:  3, name: "March" },
    APRIL     : { value:  4, name: "April" },
    MAY       : { value:  5, name: "May" },
    JUNE      : { value:  6, name: "June" },
    JULY      : { value:  7, name: "July" },
    AUGUST    : { value:  8, name: "August" },
    SEPTEMBER : { value:  9, name: "September" },
    OCTOBER   : { value: 10, name: "October" },
    NOVEMBER  : { value: 11, name: "November" },
    DECEMBER  : { value: 12, name: "December" },
}

function month(num) {
    var months = my.namespace.MonthEnum;
    for (var month in months) {
        if (month.value == num)
            return month.name;
    }

    return false;
}

答案 5 :(得分:0)

所有答案直接/间接暗示1解决方案 - “HASH”

你需要建立一个“HASH”。