Is there a way I can define a const in the constructor of a class?
I tried this:
class Foo {
    constructor () {
        const bar = 42;
    }
    getBar = () => {
        return this.bar;
    }
}
But
var a = new Foo();
console.log ( a.getBar() );
returns undefined.
You use static read-only properties to declare constant values that are scoped to a class.
class Foo {
    static get BAR() {
        return 42;
    }
}
console.log(Foo.BAR); // print 42.
Foo.BAR = 43; // triggers an error
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With