What is the correct way of declaring and using constants in Lightning Components.
I am seeing different behaviours depending on organisations I am in.
Basic way of having pass-by-value and immutable variables in JS is:
let state = [1,2,3]
const initState = Object.assign([], state)
However, after assigning both to different aura attributes, I am seeing somehow initState getting modified too, which should be impossible in first place for two reasons - it's a constant AND it's been cloned.
Does Lightning framework does something to override this behaviour?
Edit: The most obvious solution could be using unbound expressions in default
parameter on aura:attribute
:
<aura:attribute type="Object[]" name="data"/>
<aura:attribute type="Object[]" name="initData" default="{#v.data}"/>
Unfortunately that doesn't seem to work. Possibly because it assigns initial state of undefined and stop further assignments.