简体   繁体   English

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

[英]Is there a more efficient way to write this function?

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. jQuery的/的JavaScript。

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

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]

}

Or, you could do it the Wrong Way: 或者,你可以做错误的方式:

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

Look how short it is! 看它有多短! :) :)

Alas, there's a strong chance this will break in various browsers and countries. 唉,这种情况很有可能会在各种浏览器和国家中爆发。 Alternatively, it might translate the month names for you automatically. 或者,它可能会自动为您翻译月份名称。

Anyway, don't do that. 无论如何,不​​要这样做。

You're welcome. 别客气。

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

Here is an example of how to do it using the module pattern (because that's what all the cool kids do these days): 这是一个如何使用模块模式进行的示例(因为这就是所有酷孩子最近所做的事情):

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"));

I haven't touched Javascript in a while, so the syntax might be whacky, but I'm pretty sure this should work. 我有一段时间没有触及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;
}

All answers directly/indirectly imply 1 solution - "HASH" 所有答案直接/间接暗示1解决方案 - “HASH”

You need to build a "HASH". 你需要建立一个“HASH”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM