通过参考混淆分配 [英] Assignment by reference confusion

查看:74
本文介绍了通过参考混淆分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个'交易'对象,其中包含对orderBook.BTCUSDT的引用。我的意图是在orderBook.BTCUSDT更改时更改交易。

I have a 'trades' object that includes a reference to orderBook.BTCUSDT. My intention is to change 'trades' when orderBook.BTCUSDT is changed.

但是,更改orderBook.BTCUSDT不起作用。但改变orderBook.BTCUSDT.asks的确如此。

However, changing orderBook.BTCUSDT does not work. But changing orderBook.BTCUSDT.asks does.

为什么?

orderBook = {'BTCUSDT': {'asks':[1,2,3,5], 'bids':[6,7,8,9]}};
trades = {"one": orderBook.BTCUSDT};
orderBook.BTCUSDT = 1234; // does not work
console.log(trades);

/* Output:
{
  "one": {
    "asks": [
      1,
      2,
      3,
      5
    ],
    "bids": [
      6,
      7,
      8,
      9
    ]
  }
}
*/

orderBook = {'BTCUSDT': {'asks':[1,2,3,5], 'bids':[6,7,8,9]}};
trades = {"one": orderBook.BTCUSDT};
orderBook.BTCUSDT.asks = 1234; // works
console.log(trades);

/* Output:
{
  "one": {
    "asks": 1234,
    "bids": [
      6,
      7,
      8,
      9
    ]
  }
}
*/

在Axiac和Artur回复后进行修改

在阅读Axiac和Artur的回复后,我找到了另一种提问方式。为什么第一个代码块工作但不是第二个?为什么我必须使用价格为对象添加另一个级别?似乎两者都试图做同样的事情(用另一个对象替换对象但保留引用),只是在不同的级别。

After reading responses from Axiac and Artur, I found another way to ask the question. Why does the first code block work but not the second? Why should I have to add another level to the object by using 'prices'? Seems like both are trying to do the same thing (replace an object with another object but keep the reference), just at different levels.

orderBook = {BTCUSDT: { prices: {'asks':[1,2,3,5], 'bids':[6,7,8,9]}}};
trades = {one: orderBook.BTCUSDT};
orderBook.BTCUSDT.prices = {'asks':[11,12,13,15], 'bids':[16,17,18,19]};  // trades.one.BTCUSDT.prices is updated as expected 
console.log(trades); 

orderBook = {BTCUSDT: {'asks':[1,2,3,5], 'bids':[6,7,8,9]}};
trades = {one: orderBook.BTCUSDT};
orderBook.BTCUSDT = {'asks':[11,12,13,15], 'bids':[16,17,18,19]};  // trades.one.BTCUSDT is NOT updated as expected 
console.log(trades); 

编辑:变异与重新分配

我相信我在这里找到答案帖子

在上面的两个代码块中,trades.one设置为orderBook.BTCUSDT。

In both code blocks above, trades.one is set to orderBook.BTCUSDT.

在第二个代码中块,orderBook.BTCUSDT正在重新分配与第三行,而在第一个代码块orderBook.BTCUSDT正在突变在第三行。更改orderBook.BTCUSDT.prices是一个突变,因此引用不会丢失。但是,对于第二个代码块,重新分配会破坏引用。

In the second code block, orderBook.BTCUSDT is being reassigned with the third line whereas in the first code block orderBook.BTCUSDT is being mutated in the third line. Changing orderBook.BTCUSDT.prices is a mutation so the reference is not lost. However, with the second code block, the reassignment breaks the reference.

这就是axiac和Artur在没有明确讨论变异VS重新分配的情况下所说的话。

This is what axiac and Artur were also saying without explicitly discussing mutation VS reassignment.

推荐答案

此声明:

trades = {"one": orderBook.BTCUSDT};

使 trades.one 参考相同的对象为 orderBook.BTCUSDT do(具有属性的对象出价)。这样,可以使用两个变量( trades.one orderBook.BTCUSDT )访问对象。

makes trades.one refer to the same object as orderBook.BTCUSDT do (an object having the properties asks and bids). This way, the object can be accessed using two variables (trades.one and orderBook.BTCUSDT).

trades.one orderBook.BTCUSDT 是不同的实体,他们没有任何关系。恰好在上面的陈述之后它们指向同一个对象。

trades.one and orderBook.BTCUSDT are different entities and they are not related in any way. It just happens that after the statement above they point to the same object.

下一个陈述:

orderBook.BTCUSDT = 1234; // does not work

orderBook.BTCUSDT中放入不同的值并打破它与对象之间的链接。现在只能使用交易访问具有要求出价属性的对象。一个变量。

puts a different value in orderBook.BTCUSDT and breaks the link between it and the object. The object having the asks and bids properties can now be accessed only by using the trades.one variable.

这篇关于通过参考混淆分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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