为什么这个 Javascript 示例复制变量值而不是通过引用传递?

发布于 2024-10-07 17:08:43 字数 953 浏览 2 评论 0原文

Javascript The Good Parts 中,它指出:

alt text

所以我期望以下代码示例输出1001,因为“对象永远不会被复制,而是通过引用传递”,那么为什么它会输出0000

var page_item = {
  id_code : 'welcome',
  title : 'Welcome',
  access_groups : {
      developer : '0010',
      administrator : '0100'
  }
};
page_item.access_groups.member = '0000';
var member = page_item.access_groups.member;
member = '1001';

$('p#test').html(page_item.access_groups.member); //should be "1001" but is "0000"

添加:

@Gareth @David,谢谢,这就是我试图在这个例子中展示的内容,有效:

var page_item = {
  id_code : 'welcome',
  title : 'Welcome',
  access_groups : {
      developer : '0010',
      administrator : '0100'
  }
};
var page_item2 = page_item;
page_item2.access_groups.developer = '1001';

$('p#test').html(page_item.access_groups.developer); //is '1001'

In Javascript The Good Parts, it states:

alt text

So I would expect the following code example to output 1001 since "objects are never copied but passed around by reference", so why does it output 0000?

var page_item = {
  id_code : 'welcome',
  title : 'Welcome',
  access_groups : {
      developer : '0010',
      administrator : '0100'
  }
};
page_item.access_groups.member = '0000';
var member = page_item.access_groups.member;
member = '1001';

$('p#test').html(page_item.access_groups.member); //should be "1001" but is "0000"

Added:

@Gareth @David, thanks, this is what I was trying to show in this example, works:

var page_item = {
  id_code : 'welcome',
  title : 'Welcome',
  access_groups : {
      developer : '0010',
      administrator : '0100'
  }
};
var page_item2 = page_item;
page_item2.access_groups.developer = '1001';

$('p#test').html(page_item.access_groups.developer); //is '1001'

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

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

发布评论

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

评论(3

剩一世无双 2024-10-14 17:08:43

不要考虑 C++ 上下文中的引用传递,因为它不一样。

var member = page_item.access_groups.member // Sets member to this value
member = '1001'; // Now sets it to another value

如果字符串上有一个方法可以改变它们,那么这个:

member.removeLastLetter();

将改变page_item.access_groups.member。但是,使用 = 您正在更改变量的引用,而不是它之前引用的对象

Don't think of pass-by-reference in the C++ context, because it's not the same.

var member = page_item.access_groups.member // Sets member to this value
member = '1001'; // Now sets it to another value

If there was a method on strings which changed them, then this:

member.removeLastLetter();

would alter page_item.access_groups.member. However, with your = you are changing the variable's reference, not the object it previously referenced

三生殊途 2024-10-14 17:08:43

因为 page_item.access_groups.member 是一个字符串而不是一个对象。

Because page_item.access_groups.member is a string and not an Object.

滿滿的愛 2024-10-14 17:08:43

这可能会受到 JS-Gurus 的攻击,但基本上它是这样的:

对象是通过引用传递的。
字符串(数字等......基本上是一维变量)按值传递。

我确实尝试并理解了关于数据类型的冗长解释,但我确实需要完成一些工作,但没有时间更仔细地研究它。

This is probably getting bashed by JS-Gurus but basically it goes down like this:

Objects are passed by reference.
Strings (numbers, etc... basically 1 dimensional variables) are passed by value.

I did try and understand the lengthy explanations on data types but I seriously needed some work done and haven't gotten time to look at it more closely.

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