Alternative to static property in a class? #1690
Replies: 4 comments
-
Posted at 2024-06-17 by @gfwilliams Thanks - yes, it looks like there's some issue with Espruino there. It's a strange one as I added it in 2v22 and was pretty sure it was working at that point (but then I wasn't working on getters). I just filed an issue in espruino/Espruino#2517 I feel like having a getter to handle this is probably overkill though? You could have a static propery like
I think that's what you were saying? The JS classes are effectively just shorthand for writing out object oriented JS as functions, so if you do want static fields and want to target existing firmwares before I have fixed the issue, you can fix and match - so:
should all work ok? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2024-06-17 by @gfwilliams Just fixed - turns out if you add the ... but I'd still do as I said above and it'll work in 2v21 and earlier |
Beta Was this translation helpful? Give feedback.
-
Posted at 2024-06-17 by AkosLukacs Thanks! But now the Espruino IDE doesn't like that (see the picture) Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2024-06-19 by @gfwilliams I think it must be a reasonably new addition to the language - why it wasn't in Espruino before. And Acorn/Tern/JSLint in the IDE are old enough that they don't support it either - so I guess they need updating at some point as well. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2024-06-15 by AkosLukacs
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?
Beta Was this translation helpful? Give feedback.
All reactions