Alternative to static property in a class?

Posted on
  • 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?

  • 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 https://github.com/espruino/Espruino/iss­ues/2517

    I feel like having a getter to handle this is probably overkill though?

    You could have a static propery like GP8403.CH1="CH1"; and so on, and then:

    const channels = { CH0: data_for_ch, CH1: data_for_ch ];
    if (!channels[channel]) throw new Exception("Invalid channel");
    

    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:

    class GP8403 {
      // ...
    };
    // static fields
    GP8403.CH1="CH1";
    // export
    exports.GP8403 = GP8403;
    

    should all work ok?

  • Just fixed - turns out if you add the static vars after the constructor it's fine.

    ... but I'd still do as I said above and it'll work in 2v21 and earlier

  • Thanks!
    Using a string is even simpler, I was kind of hang up on "what number should represent the selected channel".
    A static var just feels slightly nicer looking than adding it after the class declaration.

    But now the Espruino IDE doesn't like that (see the picture)


    1 Attachment

    • Screenshot 2024-06-17 173200.png
  • 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.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Alternative to static property in a class?

Posted by Avatar for AkosLukacs @AkosLukacs

Actions