设置值正在改变键名

发布于 2024-10-13 08:47:53 字数 374 浏览 2 评论 0原文

var test = {
    one: {},
    two: {},
};
test['two'].parent = test['one'];

我想要测试拥有什么:

test = {
   one: {},
   two: { parent: {}, }

它实际上拥有什么:

 test = {
    parent: {},
    two: { parent: {}, }

为什么 test.one 变成 test.parent?

我希望 test.two.parent 保存对 test.one 的引用。我该怎么做?

var test = {
    one: {},
    two: {},
};
test['two'].parent = test['one'];

what i want test to have:

test = {
   one: {},
   two: { parent: {}, }

what it actually has:

 test = {
    parent: {},
    two: { parent: {}, }

why does test.one become test.parent?

I want test.two.parent to hold a reference to test.one. how would i do this?

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

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

发布评论

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

评论(3

我爱人 2024-10-20 08:47:53

为什么 test.one 变成了 test.parent?

事实并非如此。结果结构为:

{
one: {},
two: { parent: {} }
}

其中 parent 引用的对象与 one 引用的对象相同。

如果你这样做:

test.one.cheese = "crackers";

...下面的结果将是“crackers”

alert(test.two.parent.cheese); // "crackers"

示例: http:// jsfiddle.net/g5chF/

why does test.one become test.parent?

It doesn't. The resulting structure is:

{
one: {},
two: { parent: {} }
}

Where parent is referencing the same object as referenced by one.

If you do:

test.one.cheese = "crackers";

...the result below will be "crackers"

alert(test.two.parent.cheese); // "crackers"

Example: http://jsfiddle.net/g5chF/

余罪 2024-10-20 08:47:53

您使用什么浏览器?这可能会有所作为。

我在 Firefox 上的 Firebug 中收到了预期的测试结果,但是我不建议混合和匹配属性访问语法(syntaxes?syntaxen?)。

而不是:

test['two'].parent = test['one'];

我会这样做:

test['two']['parent'] = test['one'];

What browser are you using? It may make a difference.

I am receiving expected results testing in Firebug on Firefox, however I wouldn't suggest mixing and matching property access syntax (syntaxes? syntaxen?).

Instead of:

test['two'].parent = test['one'];

I would do:

test['two']['parent'] = test['one'];
笑红尘 2024-10-20 08:47:53

您对该声明的作用的主张:

test['two'].parent = test['one'];

不正确。该代码已经完成了您想要的操作:它向 test.two 添加了一个“parent”属性,以便在 test.two.parent 处有一个空对象。

Your claim as to what this statement does:

test['two'].parent = test['one'];

is incorrect. The code already does what you want: it adds a "parent" attribute to test.two, so that there'll be an empty object at test.two.parent.

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