moriken at kimamass.com (2016-01-19T09:42:27.043Z)
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.
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.