There's a big warning that any array returned by advanced syntax should not be mutated....so what's the best practice for creating arrays that I do want to mutate? (and also arrays delivered by OSC messages)? Should those go in the global variable storage? Is it ok to mutate an array stored in a variable widget?
When these syntaxes return objects or arrays, mutating said objects will affect the underlying variables in memory and may lead to unexpected behavior.
The @{}
syntax, when used for anything else than the widget's value, will always return a copy of the property and is not affected by this.
Note that this warning only applies to JS{} / #{} scripts and that advanced syntaxes should not be used in regular scripting properties such as onCreate
.
There are many ways to create a copy of an object / array,
With simple objects (that contain only primitives, not objects), a shallow copy will do:
// array
var arr = [1, 2, 3]
var copy = [...arr]
// object
var obj = {a: 1, b: 2}
var ocopy = {...obj}
With nested objects/arrays, you'll probably want a deep copy (as opposed to shallow copy), it is a subject well covered on the internet, here is one way to do it (not the most efficient though):
var copy = JSON.parse(JSON.stringify(anything))
I think I need more context to answer the other questions properly.