4

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

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

Why?

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
    ]
  }
}
*/

Edit after Axiac and Artur responses

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); 

EDIT: Mutation vs Reassignment

I believe I found the answer inside this post.

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

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.

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

JasonH
  • 521
  • 1
  • 8
  • 19
  • You are assigning a value which is `Number` not an `Object`. Numbers are not mutable but Objects are hence assigned value does not reflect after updating Value from Object. If Object is assigned, after changing value, it would update. – Rayon Jul 29 '18 at 19:54
  • 1
    @Rayon In this case he is changing the pointer not the object referenced ... so even if he changed the pointer with an `Object` (as you said) it would still have no impact on the other object which was referenced by the `trades`, please check my answer, for better visualisation. – V. Sambor Jul 29 '18 at 20:17

3 Answers3

5

This statement:

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 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.

The next statement:

orderBook.BTCUSDT = 1234; // does not work

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.

axiac
  • 68,258
  • 9
  • 99
  • 134
5
orderBook = {'BTCUSDT': {'asks':[1,2,3,5], 'bids':[6,7,8,9]}};
trades = {"one": orderBook.BTCUSDT};

1. orderBook.BTCUSDT = 1234;

enter image description here

enter image description here

2.orderBook.BTCUSDT.asks = 1234;

enter image description here

enter image description here

Artur
  • 96
  • 3
  • Artur, please see my edit, which asks the question in a slightly different manner now. What software do you use to create these visualizations? – JasonH Jul 29 '18 at 21:11
3

Hopefully this will help you to understand visually why you have this behavior.

enter image description here In the first picture you can see how the objects are referenced.

enter image description here In the second picture you can see that by doing this orderBook.BTCUSDT = 1234 you cut the connection of BTCUSDT and the object it was pointing before, so it does not point anymore, but the trades is still referencing that object.

V. Sambor
  • 12,361
  • 6
  • 46
  • 65