Just defining a constant in the constructor will not attach to the instance, you have to set it with this . I assume you want immutability, so you can use getters :
class Foo { constructor () { this._bar = 42; } get bar() { return this._bar; } }
Then you can use it as usual:
const foo = new Foo(); console.log(foo.bar)
This will not cause an error when trying to change bar . You can make a mistake in the setter if you want:
class Foo { constructor () { this._bar = 42; } get bar() { return this._bar; } set bar(value) { throw new Error('bar is immutable.'); } }
source share