开发者

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.


Yes, use the month number as an index into an array of strings (month names).


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.

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"

You need to build a "HASH".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜