IIRC the difference between protected and private is that a private field works within the class but not subclasses while protected means from a subclass you can use directly `protected.whatever` even if `whatever` is not defined but it's inherited from the super class.
Private is private, and not accessible from anywhere else. At least in PHP (and I think Java too) is like that On Tue, Jan 19, 2016 at 11:15 AM, kdex <[email protected]> wrote: > @Kenta: Is your model likely to break/leak private data into foreign > contexts > once you start binding `this`? > > On the other hand: Should you ever need to bind `this` for class functions? > Right now, I'm not entirely sure whether the implementation of "protected" > can > be runtime-safe. > > About your nomenclature: Why name it `protected` rather than `private`? Am > I > missing a key difference between the two here? > > FWIW, have a look at [this]( > https://github.com/zenparsing/es-private-fields). > > On Dienstag, 19. Januar 2016 21:40:12 CET, Thomas wrote: > > Could this be achieved with decorators? > > > > > On 19 Jan 2016, at 8:31 PM, 森建 <[email protected]> wrote: > > > > > > Dear ES discuss subscribers, > > > > > > I'm Kenta Moriuchi, > > > Department of Art and Information Design > > > Kyushu University in Japan > > > > > > I propose `Protected State`. > > > > > > In ES2015: > > > > > > ```js > > > // utility > > > function createProtectedStorage() { > > > > > > const wm = new WeakMap(); > > > > > > return (self, protectedClass) => { > > > > > > const map = wm.get(self); > > > > > > if(protectedClass == null) { > > > > > > return map || wm.set(self, Object.create(null)).get(self); > > > > > > } > > > > > > const p = new protectedClass(self); > > > if(map) { > > > > > > Object.assign(p, map); > > > > > > } > > > return wm.set(self, p).get(self); > > > > > > } > > > > > > } > > > > > > const _ = createProtectedStorage(); > > > > > > > > > class Protected_A { > > > > > > constructor(publicThis) { > > > > > > this.publicThis = publicThis; > > > > > > } > > > > > > getName() { > > > > > > return `${this.publicThis.name} ${this.lastName}`; > > > > > > } > > > > > > } > > > > > > class A { > > > > > > constructor(name, lastName) { > > > > > > // protected this > > > if(new.target === A) _(this, Protected_A); > > > > > > // public property > > > this.name = name; > > > > > > // protected property > > > _(this).lastName = lastName; > > > > > > } > > > > > > callGetName() { > > > > > > // call protected method > > > return _(this).getName(); > > > > > > } > > > > > > } > > > > > > // test > > > const a = new A("foo", "bar"); > > > > > > // "foo bar" > > > console.log(a.callGetName()); > > > > > > // "foo" > > > console.log(a.name); > > > > > > // undefined > > > console.log(a.lastName); > > > > > > > > > // extends > > > class Protected_B extends Protected_A { > > > > > > constructor(publicThis) { > > > > > > super(publicThis); > > > > > > } > > > > > > getAge() { > > > > > > return this.age; > > > > > > } > > > > > > } > > > > > > class B extends A { > > > > > > constructor(name, lastName, age) { > > > > > > super(name, lastName); > > > > > > // protected this > > > if(new.target === B) _(this, Protected_B); > > > > > > // protected property > > > _(this).age = age; > > > > > > } > > > > > > callGetAge() { > > > > > > return _(this).getAge(); > > > > > > } > > > > > > } > > > > > > // test > > > const b = new B("foo", "bar", 18); > > > > > > // "foo bar" > > > console.log(b.callGetName()); > > > > > > // 18 > > > console.log(b.callGetAge()); > > > ``` > > > > > > As Syntax Suger (ES Next): > > > > > > ```js > > > class A { > > > > > > protected lastName; > > > > > > constructor(name, lastName) { > > > > > > // public property > > > this.name = name; > > > > > > // protected property > > > protected.lastName = lastName; > > > > > > } > > > > > > protected getName() { > > > > > > return `${this.name} ${protected.lastName}`; > > > > > > } > > > > > > callGetName() { > > > > > > return protected.getName(); > > > > > > } > > > > > > } > > > > > > class B extends A { > > > > > > protected age; > > > > > > constructor(name, lastName, age) { > > > > > > super(name, lastName); > > > protected.age = age; > > > > > > } > > > > > > protected getAge() { > > > > > > return protected.age; > > > > > > } > > > > > > callGetAge() { > > > > > > return protected.getAge(); > > > > > > } > > > > > > } > > > ``` > > > > > > Please discuss this proposal, thank you. > > > _______________________________________________ > > > es-discuss mailing list > > > [email protected] > > > https://mail.mozilla.org/listinfo/es-discuss > > > > _______________________________________________ > > es-discuss mailing list > > [email protected] > > https://mail.mozilla.org/listinfo/es-discuss > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

