Skip to content

Instantly share code, notes, and snippets.

@ronnieoverby
Created December 1, 2015 18:13
Show Gist options
  • Save ronnieoverby/5ad714442296f4d88c4a to your computer and use it in GitHub Desktop.
Save ronnieoverby/5ad714442296f4d88c4a to your computer and use it in GitHub Desktop.
private state in ES6 classses
const privates = Privates();
class MyClass{
constructor(a,b,c){
Object.assign(privates(this),{a,b,c})
}
sum(){
var {a,b,c} = privates(this);
return a + b + c;
}
}
function Privates() {
var map = new WeakMap();
return function (obj) {
if (!map.has(obj))
map.set(obj, {});
return map.get(obj);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment