`for(let key of [ ...Object.getOwnPropertyNames(map),
...Object.getOwnPropertySymbols(map) ])`

... or ...

`for(let key of Reflect.ownKeys(map))`

On Tue, Jan 19, 2016 at 4:10 PM, 森建 <[email protected]> wrote:

> maybe okay.
>
> ```js
> "use strict";
>
> // utility
> function createProtectedStorage() {
>   const wm = new WeakMap();
>
>   return (self, protectedClass) => {
>     const map = wm.get(self);
>
>     if(protectedClass == null) {
>       if(map) {
>         return map;
>       } else {
>         const ret = Object.create(null);
>         wm.set(self, ret);
>         return ret;
>       }
>     }
>
>     const p = new protectedClass(self);
>     if(map) {
>       for(let key of [ ...Object.getOwnPropertyNames(map),
> ...Object.getOwnPropertySymbols(map) ]) {
>
>         const descriptor = Object.getOwnPropertyDescriptor(map, key);
>         Object.defineProperty(p, key, descriptor);
>       }
>     }
>     wm.set(self, p);
>     return p;
>   }
> }
>
> 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
>     Object.defineProperty(_(this), "lastName", {
>       enumerable: false,
>       configurable: false,
>       writable: false,
>       value: lastName
>     });
>   }
>
>   callGetName() {
>     // call protected method
>     return _(this).getName();
>   }
> }
>
> // extends
> class Protected_B extends Protected_A {
>
>         getAge() {
>                 return this[Symbol.for("$$age")];
>         }
>
> }
>
> class B extends A {
>
>     constructor(name, lastName, age) {
>         super(name, lastName);
>
>         // protected this
>         if(new.target === B)    _(this, Protected_B);
>
>         // protected property
>         _(this)[Symbol.for("$$age")] = age;
>     }
>
>     callGetAge() {
>         return _(this).getAge();
>     }
>
>     // added
>     getData() {
>         return {
>             name: _(this).getName(), // Protected_A
>             [Symbol.for("$$age")]:  _(this).getAge()
>         }
>     }
>
>     getLastNameDescriptor() {
>       return Object.getOwnPropertyDescriptor(_(this), "lastName");
>     }
>
>     getAgeDescriptor() {
>       return Object.getOwnPropertyDescriptor(_(this), Symbol.for("$$age"));
>     }
>
> }
>
> // do nothing class
> class Protected_C extends Protected_B {
>
> }
>
> class C extends B {
>
>   constructor(...args) {
>     super(...args);
>     if(new.target === C)    _(this, Protected_C);
>   }
>
> }
>
> // test
> const c = new C("foo", "bar", 18);
>
> // "foo bar"
> console.log(c.callGetName());
>
> // 18
> console.log(c.callGetAge());
>
> // { "name": "foo bar", [Symbol($$age)]: 18 }
> console.log(c.getData());
>
> // { "value": "bar", "writable": false, "enumerable": false,
> "configurable": false }
> console.log(c.getLastNameDescriptor());
>
> // { "value": 18, "writable": true, "enumerable": true, "configurable":
> true }
> console.log(c.getAgeDescriptor());
> ```
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to