带参数的Javascript函数

时间:2013-07-02 19:58:28

标签: javascript

我有两个字符串变量: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;

现在,橙色日期的值应该是'十二月',不是吗?但是当我运行它时,我得到“12”作为值。所以功能不起作用。我做错了什么?

7 个答案:

答案 0 :(得分:5)

你抓到了一只红鲱鱼。

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

你的问题远不及字符串连接或与字符串连接有关;你理解得当。您需要了解Javascript是通过引用副本传递的,并且您正在更改引用的副本。见这里:Is JavaScript a pass-by-reference or pass-by-value language?

答案 1 :(得分: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);

Demo on JSFiddle

答案 2 :(得分: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);

答案 3 :(得分: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;
}

答案 4 :(得分:0)

变量month按值传递 - 也就是说,当您调用getMonthName时,month是COPY的orangemonth1,而不是变量本身。因此,当你改变月份时,你不会影响橘子。

尝试将return month添加到getMonthName

的末尾

答案 5 :(得分: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));

答案 6 :(得分:0)

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

Is JavaScript a pass-by-reference or pass-by-value language?

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

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

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

var textMonth = getMonthName(orangemonth1);