I'm seeing a strange issue in the initialisation o...
# javascript
e
I'm seeing a strange issue in the initialisation of top level variables. It looks like the generated JS defines the "var"s and then initialises them later. But the order of the initialisation can lead to strange results. I have one piece of code that uses a constant to initialise an object, but it is undefined. Constants file
Copy code
val BAT_WIDTH = 8
Other File
Copy code
private val batGeometry = BoxGeometry(BAT_WIDTH, 1.0, 1.0)
The BAT_WIDTH here is undefined. I can get around the issue by putting my constants in a singleton object, but worried about initialisation order generally now. Any docs on this or anyone know how it works? The generated JS seems to be,
Copy code
var batGeometry;
...
var BAT_WIDTH;
...
batGeometry = new BoxGeometry(BAT_WIDTH, 1.0, 1.0);
...
BAT_WIDTH = 8;
t
It is bug :(
Bug report required
😥 2
a
I have experienced this too. My workaround was to make the top level variable as a getter
Copy code
val BAT_WIDTH get() = 8
Sometimes I don't need getters, as I don't need recomputations to happend. I do endup resoting to
lazy
Copy code
private val batGeometry by lazy {BoxGeometry(BAT_WIDTH,1.0,1.0)}