0

I feel kind of stupid about this, but from what I've searched and read I understand that there is no way to create a truely private var in ecma6 classes. am I correct?

this is an examplee of a getter function in emca5, is there anyway to do this in ecma6-classes?

function foo() {
var me="foo";
    this.getPrivate() {
       return me
    }
}
var new_foo = new foo()
console.log( new_foo.me) //return undefined
console.log( new_foo.getPrivate()) //return "foo"
Shai Kimchi
  • 746
  • 1
  • 4
  • 22
  • sorry, I've re-edited it. the question stays the same, how to do this in ecma6? – Shai Kimchi Jul 20 '16 at 09:59
  • 2
    Yes, of course you can do the same thing in ES6 classes. Just move the contents of your `foo` function into the `class foo { constructor() … }`, like [here](http://stackoverflow.com/a/34871822/1048572). – Bergi Jul 20 '16 at 09:59
  • but if a new function is added to the class it will not see the vars inside the constructor. – Shai Kimchi Jul 20 '16 at 10:02
  • 3
    Just like `foo.protoype` methods were not able to in the ES5 snippet. Nothing changes in that regard. – Bergi Jul 20 '16 at 10:03
  • _"but if a new function is added to the class it will not see the vars inside the constructor"_ But it will see `getPrivate`. It's preferable to define "real" getters / setters. – a better oliver Jul 20 '16 at 10:21

0 Answers0