Not really, that's about as good as it gets. There are ways to reduce that down by a line but it would be hard to follow - really not worth it. JavaScript is actually very good at optimising things like this when it actually runs so I wouldn't worry about it.
I wasn't sure if an object reference was returned & thus updatable directly. Can't get my head around this one. I'm certain I've done something similar?!?
As I thought, the docs are clear about this - you should be using set to update the variable. I think that means that the approach you've given is an unintended outcome and so couldn't be relied upon.
I believe it is perhaps not the intention however a side affect of the Javascript language. The global/flow get() functions return the object & thus its a reference (not a copy) & therefore modifiable.
Not only would I not want to rely on that but I also think that it is much harder to follow - coming back to it in 6m time I think it might take a while to work out. Certainly if, for some reason, the documented approach is enforced, it might be difficult to spot an debug.
I am certainly no expert in all things javascript however I can see no way this could be prevented or broken for that matter. When you store an object (where ever that is) you ultimately store a reference & thus retrieve a reference. If it wasn't, you'd be setting/getting a clone/copy & then this would be a whole new problem (deep vs shallow copy).
Note: I'm not advocating this approach (its certainly less readable to those unaware of the subtleties of javascript. the getset functions are far clearer in their intent.
However, it depends how the storage mechanism works. If i were to write it, the store would primarily be memory based (writing to file when property is updated, but only reading only upon node-red startup) in which case it would still work.
well we don't mind if you wish to bend the rules as long as you are happy knowing the consequences, but probably best not to encourage others to do so if they are unaware of the gotchas.
Of course you are correct for a file based store. The functions would need to be executed.
@Petr by now hopefully you will see although yes, you can update in one line, should you ever use file back context or the implementation of get/set change, you may have problems. Stick with what you have unless you fully understand the what and the why
Guys, you are great here! Thanks
I didn’t intended to go so deep
For me (as a Pythonist, who is trying to taste js soup now) it is difficult to accept 3 lines of the code and all those curly and semicolon things. But I am trying to work hard and to accept this "new different culture"
I was thinking about to change the property of the global object directly, without the necessity to:
declare variable
call the whole object to the script node as a local object
changing the property of the local object
returning local object back to the global variable.
BTW, It looks to me, I am rewriting/resetting the whole object stored inside the global variable instead of rewriting property value only.
I was really thinking about some semantic like you see below, and, of course, trying to find some examples, I was not successful (yes, it was a long night lol)
global.set("obed07").payload.template = "bar";
global.set("obed07".payload.template:"bar");
global.set("obed07.payload.template":"bar");
// for the readers of this forum
// this is an example of the BAD syntax, dont use it
Thank you again for explanation.
BTW: how often / in which cases to use global variables? I have a small project managing 10 Sonoff mqtt POW R2 switches and I need to keep the recent state of each switch and compare it with new values from the incoming mqtt flow . Till now, the only way I have found for this is to use globals.
Haha, well that cuts both ways. I learned Python long before I learned JavaScript. In fact it was where I first really understood about object-based programming. Sadly, the Python masters completely missed some important developments in the world and that meant that Python was never integrated as well into the web world. At the same time, Node.JS came along which meant that, as a non-pro programmer, I chose a single language to cover both front- and back-end programming. Bye-bye PHP and Python (and PERL!) - I've only so much time to try and remember how to do things.
I don't like all the {}; stuff either - in fact I refuse to use ; unless they are absolutely necessary (only about 3 use cases in JavaScript that actually need a ;).
Argh! I keep forgetting that this is actually possible!!! That works absolutely fine
Except that you just got one thing slightly wrong, here is the correct answer (colons are for objects, comma's for function arguments):
Aaaa.... so no more 3 lines of code?
Is it really possible to change the property of the global variable in the one line of code within the function code? No need to define, get, change, set? Just set directly?
What about the second syntax?
flow.set("obed07.payload['template']","jim")
What do the square brackets mean? I don’t understand it.