1

我有两个字符串变量:month1Digit1 和month1Digit2。它们一起构成一个月的十进制数字(01-12),因此month1Digit1始终为0或1,而month1Digit2可以是0以外的任何数字。现在我有其中的几个,如month2Digit1等。我想要一个可以从这些变量中确定月份名称的函数。但我不想仅仅因为它有不同的变量就为每个组编写一个单独的函数。从四处搜索看起来我需要用参数做一个函数,但我不太确定它是如何工作的。我尝试了以下方法:

var month1Digit1 = "1";
var month1Digit2 = "2";

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

var orangemonth1 = month1Digit1 + month1Digit2;
getMonthName(orangemonth1);
orangedate = orangemonth1;

现在从这里来看,orangedate 的值应该是 'December',不是吗?但是当我运行它时,我得到“12”作为值。所以该功能不起作用。我究竟做错了什么?

4

7 回答 7

5

你钓到了一条红鲱鱼。

function foo(in) {
    in = 2;
}
var a = 1;
foo(a);
console.log(a); // prints 1

您的问题与字符串连接相去甚远或相关;你理解正确。您需要了解 Javascript 是通过引用副本传递的,并且您正在更改引用的副本。请参阅此处:JavaScript 是按引用传递还是按值传递语言?

于 2013-07-02T20:02:21.167 回答
2

我会使用一个数组将月份映射到您使用的编号系统并从函数返回结果:

var month1Digit1 = '0';
var month1Digit2 = '7';

var monthMap = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

function getMonthName (input) {
    var monthNumber = parseInt(input);
    return monthMap[monthNumber];
}

var orangemonth1 = month1Digit1 + month1Digit2;
orangedate = getMonthName(orangemonth1);

alert(orangedate);

JSFiddle 上的演示

于 2013-07-02T20:05:47.483 回答
0

我认为你错过了变量是通过引用还是通过值传递到javascript中的函数的问题。答案并不像人们想象的那么简单。看看这个问题。

JavaScript 是按引用传递还是按值传递语言?

无论如何,如果你想让这个工作,而不是假设你的变量已经在函数中被改变,显式地返回你的新值而不是month

function getMonthName (month) { if (month == "1") { month = "January" }... etc etc return month; }

此后,您需要像这样调用您的函数:

var textMonth = getMonthName(orangemonth1);

于 2013-07-02T20:18:15.343 回答
0

问题是您没有返回任何变量,也没有分配它。
在您的情况下,该值仅更改为函数。
尝试这个:

var month1Digit1 = "1";
var month1Digit2 = "2";

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

    return (month);
}

var orangemonth1 = month1Digit1 + month1Digit2;
orangedate = getMonthName(orangemonth1);
于 2013-07-02T20:01:16.240 回答
0

你可以试试这个;

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

    return months[parseInt(month)];
}

console.log(getMonthName(1));
于 2013-07-02T20:12:43.563 回答
0

你没有从你的函数中返回任何东西。

function getMonthName (month) {
    if (month == "1") { month = "January" }
    else if (month == "2") { month = "February" }
    else if (month == "3") { month = "March" }
    else if (month == "4") { month = "April" }
    else if (month == "5") { month = "May" }
    else if (month == "6") { month = "June" }
    else if (month == "7") { month = "July" }
    else if (month == "8") { month = "August" }
    else if (month == "9") { month = "September" }
    else if (month == "10") { month = "October" }
    else if (month == "11") { month = "November" }
    else if (month == "12") { month = "December" }
    return month;
}
于 2013-07-02T20:02:44.700 回答
0

变量month是按值传递的——也就是说,当您调用 getMonthName 时,月份是 orangemonth1 的副本,而不是变量本身。因此,当您更改月份时,您不会影响橙色。

尝试添加return month到 getMonthName 的末尾

于 2013-07-02T20:03:08.913 回答