-2

我有一段代码如下。

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)。但是如何在参数中传递索引的索引。

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

4

2 回答 2

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"
});
于 2013-10-15T06:27:57.407 回答
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");
于 2013-10-15T06:27:47.317 回答