javascript - Define a const in class constructor (ES6) -


this question has answer here:

is there way can define const in constructor of class?

i tried this:

class foo {     constructor () {         const bar = 42;     }      getbar = () => {         return this.bar;     } } 

but

var = new foo(); console.log ( a.getbar() ); 

returns undefined.

you use static read-only properties declare constant values scoped class.

class foo {     static bar() {         return 42;     } }  console.log(foo.bar); // print 42. foo.bar = 43; // triggers error 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

post - imageshack API cURL -