Directly modify context as reference

Hi, working with context (context,flow or global) we directly access and modify them unless we don't use RED.util.cloneMessage. So if for instance we have

flow.set("mytest",{ "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":[10,20,30]})
doing

var mynewtest = flow.get("mytest")
mynewtest.C = ["a","b","c"]

it results in
flow.get("mytest") = { "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":["a","b","c"]}
so mynewtest modify context mytest as expected

But if we do

var mynewtest = flow.get("mytest.C")
mynewtest = ["a","b","c"]

it results in
flow.get("mytest") = { "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":[10,20,30]})
instead of
flow.get("mytest") = { "A":[1,2,3], "B":{"value1":"B1","value2":"B2"},"C":["a","b","c"]}
In this case mynewtest doesn't modify context mytest. Is that correct?

Correct. The first line sets mynewtext to point to the object in the context memory.
The second line does not change a property of mynewtext (which would change the value in context) but simply sets the value of mynewtest to point to a newly created object containing the array.

By the way, it is best not to use var nowadays. One should use const or let. Your favourite search engine will tell you why if you want to know.

Thanks so much @Colin

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.