Created
December 1, 2015 18:13
-
-
Save ronnieoverby/5ad714442296f4d88c4a to your computer and use it in GitHub Desktop.
private state in ES6 classses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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