在另一个对象中重用对象

发布于 2024-09-24 17:11:02 字数 665 浏览 6 评论 0原文

我有一个对象,其中包含另一个带有翻译的对象。它看起来像这样:

var parent = { //parent object with a bunch of stuff in it
    countryAlerts:{ //area for country alerts
        "USA":{ //country
            en:"FAST", //alert text in English
            es:"RAPIDO" //alert text in Spanish
        },
        "Japan":{
            en:"FAST",
            es:"RAPIDO"
        }
    }
}

我想做这样的事情:

var parent = {
    countryAlerts:{
        "USA":fast,
        "Japan":fast
    }
}

var fast = {
    en:"FAST",
    es:"RAPIDO"
}

但我似乎无法让它工作。我想引用这样的对象:parent.countryAlerts.USA.LANGUAGE

任何帮助表示赞赏。

谢谢!

I have an object that contains another object with translations. It looks like this:

var parent = { //parent object with a bunch of stuff in it
    countryAlerts:{ //area for country alerts
        "USA":{ //country
            en:"FAST", //alert text in English
            es:"RAPIDO" //alert text in Spanish
        },
        "Japan":{
            en:"FAST",
            es:"RAPIDO"
        }
    }
}

I would like to do something like this:

var parent = {
    countryAlerts:{
        "USA":fast,
        "Japan":fast
    }
}

var fast = {
    en:"FAST",
    es:"RAPIDO"
}

I can't seem to get it to work though. I would like to refer to the object like this: parent.countryAlerts.USA.LANGUAGE.

Any help is appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2024-10-01 17:11:02

是的,这是可能的,您只需首先定义 fast

var fast = {
    en: "FAST",
    es: "RAPIDO"
};
var parent = {
    countryAlerts: {
        "USA" : fast,
        "Japan" : fast
    }
};

var lang = "en";

alert(parent.countryAlerts.USA[lang]); // "FAST"

请注意使用方括号表示法通过变量名称访问对象的成员。

编辑:回应评论:

啊啊,这太愚蠢了。谢谢!我现在还有一个问题。假设我想在父对象中快速存储。

所以你的意思是,像这样(伪代码)?

var parent = {
    fast : {
        en : "FAST",
        es : "RAPIDO"
    },
    countryAlerts : 
        "USA" : fast    // ** magic here
        "Japan" : fast  // **
    }
};

简而言之,你不能用这样的一句话来做到这一点。最好的选择是将其移至多个语句。

var parent = {
    fast : {
        en : "FAST",
        es : "RAPIDO"
    }
};
parent.countryAlerts = {
    "USA" : parent.fast,
    "Japan" : parent.fast
};

Yes that is possible, you just have to define fast first:

var fast = {
    en: "FAST",
    es: "RAPIDO"
};
var parent = {
    countryAlerts: {
        "USA" : fast,
        "Japan" : fast
    }
};

var lang = "en";

alert(parent.countryAlerts.USA[lang]); // "FAST"

Note the use of the square bracket notation for accessing an object's members by a variable name.

Edit: In response to the comment:

Ahh, that was silly. Thanks! I have another question now. Let's say I want to store fast in the parent object.

So you mean, like this (pseudo-code)?

var parent = {
    fast : {
        en : "FAST",
        es : "RAPIDO"
    },
    countryAlerts : 
        "USA" : fast    // ** magic here
        "Japan" : fast  // **
    }
};

In short, you can't do it in one statement like that. Your best bet would be to move it to multiple statements.

var parent = {
    fast : {
        en : "FAST",
        es : "RAPIDO"
    }
};
parent.countryAlerts = {
    "USA" : parent.fast,
    "Japan" : parent.fast
};
不…忘初心 2024-10-01 17:11:02

为了您的后续行动:

var parent = new (function()
{
    var fast = {
      en: "FAST",
      es: "RAPIDO"
    };

    this.countryAlerts = {
        "USA" : fast,
        "Japan" : fast
    };
})();

For your follow-up:

var parent = new (function()
{
    var fast = {
      en: "FAST",
      es: "RAPIDO"
    };

    this.countryAlerts = {
        "USA" : fast,
        "Japan" : fast
    };
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文