Assignment by reference confusion

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Assignment by reference confusion



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





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
1 hour ago


Number


Object





@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
1 hour ago


Object


trades




3 Answers
3


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





uuuuh fancy +1 :D
– Pilan
1 hour ago





Artur, please see my edit, which asks the question in a slightly different manner now. What software do you use to create these visualizations?
– JLH
22 mins ago





pythontutor.com/javascript.html#mode=edit
– Artur
20 mins ago





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


orderBook.BTCUSDT


asks


bids


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.


trades.one


orderBook.BTCUSDT



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.


orderBook.BTCUSDT


asks


bids


trades.one





axiac, please see my edit, which asks the question in a slightly different manner now.
– JLH
21 mins ago



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.


orderBook.BTCUSDT = 1234


BTCUSDT


trades






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived