在javascript中将数组索引作为参数传递

时间:2013-10-15 05:59:43

标签: javascript arrays

我有一段代码如下。

var aMessages = new Array();
aMessages["gender"] = new Array();
aMessages["gender"]["blank"] = new Array();
aMessages["gender"]["blank"]["Default"] = "Please select the gender of the person being registered on the site.";
aMessages["gender"]["blank"]["Self"] = "Please select your gender.";
aMessages["gender"]["blank"]["Son"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Daughter"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Brother"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Sister"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Friend"] = "Please select your friend's gender.";
aMessages["gender"]["blank"]["Relative"] = "Please select your relative's gender.";
aMessages["gender"]["invalid"] = "Please choose relevant gender.";

function displayMessage(field, errorCase){
    console.log(errorCase, field, "here");
    console.log(aMessages[field][errorCase]);
}

err = 'invalid';
displayMessage("gender", err);

所以我将字段和错误大小写传递给函数,函数做了一些事情,现在假设它打印消息。对于一个等级gender (field) -> err (errorcase)来说这很好。但是如何在参数中传递索引的索引。

是否有任何解决方法。还建议任何其他好的approch。

2 个答案:

答案 0 :(得分:2)

最灵活的方式是使用arguments,如此问题中所述:JavaScript variable number of arguments to function

然而,一个常见的JavaScript实践是为了可读性而使用对象文字,并且不必担心参数的顺序。您可以在jQuery UI API中看到许多这样的示例,例如:.effect( options )

这是一个开始(jsfiddle):

function displayMessage(field, errorCase) {
    if (typeof errorCase === "string") {
        console.log(errorCase, field, "here");
        console.log(aMessages[field][errorCase]);
    } else {
        // You should ensure the errorCase object literal has these properties before accessing them
        console.log(errorCase.err, field, "there");
        console.log(errorCase.relation, field, "there again");
        console.log(aMessages[field][errorCase.err][errorCase.relation]);
    }
}

displayMessage("gender", {
    err: "blank",
    relation: "Sister"
});

答案 1 :(得分:0)

这提供了你想要的东西吗?

var aMessages = new Array();
aMessages["gender"] = new Array();
aMessages["gender"]["blank"] = new Array();
aMessages["gender"]["blank"]["Default"] = "Please select the gender of the person being registered on the site.";
aMessages["gender"]["blank"]["Self"] = "Please select your gender.";
aMessages["gender"]["blank"]["Son"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Daughter"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Brother"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Sister"] = "Gender of the member is required for registration.";
aMessages["gender"]["blank"]["Friend"] = "Please select your friend's gender.";
aMessages["gender"]["blank"]["Relative"] = "Please select your relative's gender.";
aMessages["gender"]["invalid"] = "Please choose relevant gender.";

function displayMessage(field, errorCase){
    console.log(errorCase, field, "here");

    var argc = arguments.length;
    if(argc >= 3) {
      console.log(aMessages[field][errorCase][arguments[2]]);
    }
    else {
      console.log(aMessages[field][errorCase]);
    }
}

err = 'invalid';
displayMessage("gender", err);

err = 'blank';
displayMessage("gender", err, "Daughter");