Message from JavaScript discussions

June 2017

— I guess it depends on use case

— 

const priv = new WeakMap();

class MyClass {
constructor () {
priv.set(this, { value: 42 });
}
digitSum () {
return String(priv.get(this).value)
.split('').reduce((sum, digit) =>
sum + digit, 0);
}
}

— Like so

— Once this gets gc'ed, { value: 42 } can also be safely gc'ed

— Here it is btw https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols

Message permanent page

— Well shit

— Anyways, that's the two ways I know of to shim private vars

— It seems we both surprise each other right now! XD

— Nothing outside this scope has access to the weakmap, hence private vars

— Anyways, what you said makes sense, that the new keyword would not neccesarily introduce any bad things or cons.

Message permanent page

— Although just doing closures is way easier

— It always is. More developers should take advantage of (abuse!) JS scoping rules