English 中文(简体)
是否有可能简化这一javascript?
原标题:Is it possible to simplify this javascript?

我是新鲜的,这样说,我不知道多少。 我有以下内容,我想使其更加简单。 任何建议都会受到赞赏。

if (entity == "house") {
    primaryKey = store.getItem( accountID ) + "02" + "00";
    table = "Content";
}
if (entity == "street") {
    primaryKey = store.getItem( accountID ) + "0000";
    table = "Content";
}
if (entity == "city") {
    var primaryKey = store.getItem( categoryID );
    table = "Reference";
}
if (entity == "location") {
    primaryKey = "0001" + store.getItem( examID );
    table = "Content";
}
最佳回答

你们可以采取由桌上驱动的做法,不重复成像:

var lookupInfo = {
    house: {id: "accountID", prefix, "", suffix: "0200", table: "Content"},
    street: {id: "accountID", prefix: "", suffix: "0000", table: "Content"},
    city: {id: "categoryID", prefix: "", suffix: "", table: "Reference"},
    location: {id: "examID", prefix: "0001", suffix: "", table: "Content"}
};

var primaryKey, data = lookupInfo[entity];
if (data) {
    primaryKey = data.prefix + store.getItem(data.id) + data.suffix;
    table = data.table;
}

除了尽量减少守则,不重复任何守则之外,还很容易在表格中增加更多的选择,而不必编写任何补充守则。


或者,数据表可以稍加紧凑,但并非像代表(从纯粹的方案拟订观点来看,由于编码硬数不变)。

var lookupInfo = {
    house: ["accountID", "", "0200", "Content"],
    street: ["accountID", "", "0000", "Content"],
    city: ["categoryID", "", "", "Reference"],
    location: ["examID", "0001", "", "Content"]
};

var primaryKey, data = lookupInfo[entity];
if (data) {
    primaryKey = data[1] + store.getItem(data[0]) + data[2];
    table = data[3];
}

无论是哪种方式,你们都想利用实体的考察表,然后就每个实体的不同价值采取表格驱动的办法。

问题回答

You can use a switch statement. https://developer.mozilla.org/en/JavaScript/Reference/Statements/switch

var primaryKey;
var table = "Content";
switch (entity) {
    case "house":
        primaryKey = store.getItem( accountID ) + "0200";
        break;
    case "street":
        primaryKey = store.getItem( accountID ) + "0000";
        break;
    case "city":
        primaryKey = store.getItem( categoryID );
        table = "Reference";
        break;
    case "location":
        primaryKey = "0001" + store.getItem( examID );
        break;
    default:
        // do nothing
}

这仍然ver,但更容易阅读。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签