LOG

WeakMapでクラスの似非プライベートメンバ実装

どうしてprivateがないんじゃあ・・・

let privateMap = new WeakMap();
let getPrivate = (that)=>{
    return privateMap.get(that);
}

class MyClass{
    constructor(){
        privateMap.set(this,Object.create(null));
        let _   = getPrivate(this);
        _.prop1 = Math.random() * 10;
    }
    public_func(){
        console.log(getPrivate(this).prop1);
    }
}

let instance1 = new MyClass();
let instance2 = new MyClass();

instance1.public_func();
instance2.public_func();

参考:Managing the private data of ES6 classes