Looks like static properties in classes are not supported?
class Foo {
static get CH0() { return 0xdeadbeef; }
constructor() {
print('nothing to see here')
}
}
In a browser Foo.CH0 returns 0xdeadbeef as expected.
In Espruino Foo.CH0 returns undefined. No error either, so it's parsed, but ignored / lost somewhere? It's not on the instance if you create a new instance of the class either.
Why I want to do this? I'm writing a driver for a dual DAC, and while playing around, I found it's sort of easy to mix up the voltage and channel. Like dac.setVoltage(0, 1), or dac.setVoltage(1, 0) sets channel one to zero volts? And since this DAC (GP8403) has a feature of setting both channels at the same time, the channel parameter has 3 valid values.
So I was thinking about some magic values as constants, that would be outside the allowed voltage range. something like dac.setVoltage(0, GP8403.CH1), and if GP8403.CH1 is outside of the 0..10V range, I can throw on invalid input, and any bug should be caught early. I know, might be overly defensive...
Non-static getter does work. So dac.setVoltage(0, dac.CH1) works just fine.
And of course I could define a constant, and export that as well from my module. But this approach doesn't feel really nice I guess. Or am I missing something?
So, what would you recommend? Am I overthinking this?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Looks like static properties in classes are not supported?
In a browser
Foo.CH0
returns0xdeadbeef
as expected.In Espruino
Foo.CH0
returnsundefined
. No error either, so it's parsed, but ignored / lost somewhere? It's not on the instance if you create a new instance of the class either.Why I want to do this? I'm writing a driver for a dual DAC, and while playing around, I found it's sort of easy to mix up the voltage and channel. Like
dac.setVoltage(0, 1)
, ordac.setVoltage(1, 0)
sets channel one to zero volts? And since this DAC (GP8403) has a feature of setting both channels at the same time, thechannel
parameter has 3 valid values.So I was thinking about some magic values as constants, that would be outside the allowed voltage range. something like
dac.setVoltage(0, GP8403.CH1)
, and ifGP8403.CH1
is outside of the 0..10V range, I can throw on invalid input, and any bug should be caught early. I know, might be overly defensive...Non-static getter does work. So
dac.setVoltage(0, dac.CH1)
works just fine.And of course I could define a constant, and export that as well from my module. But this approach doesn't feel really nice I guess. Or am I missing something?
So, what would you recommend? Am I overthinking this?