Javascript指针/参考疯狂。有人可以解释一下吗? [英] Javascript pointer/reference craziness. Can someone explain this?

查看:151
本文介绍了Javascript指针/参考疯狂。有人可以解释一下吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript通过引用传递对象。这很有道理。但是一旦你开始操纵这些物体,一切都会以一种看起来不直观的方式发挥作用。让我举一个例子:

Javascript passes objects by reference. This makes perfect sense. But once you start manipulating those objects, everything acts in a way that seem unintuitive. Let me offer an example:

var a, b;

a = {}
b = a;
a['one'] = {};

console.log( JSON.stringify(a) );
// outputs: {"one":{}}

console.log( JSON.stringify(b) );
// outputs: {"one":{}}

这一切都很好,好,因为现在 b 有一个指向 a 的指针,因此预计会将内容分配给 a 也会影响 b

This is all well and good because now b has a pointer to a so it's expected that assigning stuff to a will also affect b.

但是如果我这样做:

a = a['one'];

console.log( JSON.stringify(a) );
// outputs: {}

console.log( JSON.stringify(b) );
// outputs: {"one":{}}

这对我来说很惊讶。我希望 a b 仍然相同(并且 {} ,因为 a ['one'] 之前已设置为 {} a 设置为 a ['one'] )。

This is surprising to me. I'd expect a and b to still be the same (and to be {} since a['one'] was previously set to {} and a was set to a['one']).

但事实并非如此。似乎 a 在分配给新的东西时失去对 b 的引用,但是 b 维护 a 的值设置为 a 之前失去对<$的引用c $ c> b 。

But that's not the case. It appears that a loses its reference to b when it's assigned to something new, but b maintains the value that a was set to prior to a loosing its reference to b.

但是如果我这样做:

a['two'] = 2;

console.log( JSON.stringify(a) );
// outputs: {"two":2}

console.log( JSON.stringify(b) );
// outputs: {"one":{"two":2}}

什么? a 明显失去了对 b 的引用,但是 b 似乎仍然有一些参考 a

What? a has clearly lost it's reference to b, but b seems to still have some reference to a.

空对象 {} 指向内存中的某个位置,因此引用它的每个变量现在都指向同一个位置?

Does the empty object {} point to some place in memory so every variable referencing it is now pointing to the same place?

能够牢牢掌握它的人能解释一下吗?给我?

Can someone with a firm grasp on this explain it to me?

推荐答案

逐行跟踪你的例子:

a = {}

a 现在引用新对象。

b = a;

b 现在引用同一个对象 a 引用。请注意,它不会引用 a

b now references the same object that a references. Note that it does not reference a.

a['one'] = {};

新对象现在有一个索引'one'引用另一个新对象。

The new object now has an index 'one' that references another new object.

当你这样做时

a = a['one'];

您正在设置 a 以引用 a ['one'] ,这是你在 a ['one'] = {} 。 b 仍然使用 a = {} 引用您创建的对象。

You are setting a to refer to a['one'], which is that new object you created when you did a['one'] = {}. b still references the object you created with a = {}.

当您说 a 已失去对 b 的引用时,您会感到困惑,因为 a 不是指 b ,反之亦然。 a b 引用对象,可以使它们引用其他对象。像这样:

You are confusing the issue when you say "a has lost its reference to b" because a does not refer to b , nor vice versa. a and b refer to objects, and they can be made to refer to other objects. Like this:

a = {}; b = a ,你得到

a
 \
  \
   { }
  /
 /
b

然后与 a ['one'] = {} 你得到

a
 \
  \
   { one: { } }
  /
 /
b

然后用 a = a ['one'] 你得到

a - - - - 
          \
   { one: { } }
  /
 /
b

这篇关于Javascript指针/参考疯狂。有人可以解释一下吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆