设置值正在改变键名
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实并非如此。结果结构为:
其中
parent
引用的对象与one
引用的对象相同。如果你这样做:
...下面的结果将是“crackers”
示例: http:// jsfiddle.net/g5chF/
It doesn't. The resulting structure is:
Where
parent
is referencing the same object as referenced byone
.If you do:
...the result below will be "crackers"
Example: http://jsfiddle.net/g5chF/
您使用什么浏览器?这可能会有所作为。
我在 Firefox 上的 Firebug 中收到了预期的测试结果,但是我不建议混合和匹配属性访问语法(syntaxes?syntaxen?)。
而不是:
我会这样做:
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:
I would do:
您对该声明的作用的主张:
不正确。该代码已经完成了您想要的操作:它向
test.two
添加了一个“parent”属性,以便在test.two.parent
处有一个空对象。Your claim as to what this statement does:
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 attest.two.parent
.