# Private members in ES6 classes *"Let's create an ES6 class!"* you say. *"Let's give it a private variable `x`."* ```js class Foo { constructor(x) { this.x = x; } getX() { return this.x; } } ``` *"Please."* I respond, rolling my eyes. *"That `x` instance isn't REALLY private. It's right there for the whole world to read and write!"* *"We'll just prepended it with an underscore,"* you retort. *"People will never use it because it's ugly and that underscore will scare them off!"* ```js class Foo { constructor(x) { this._x = x; } getX() { return this._x; } } ``` *"It is ugly,"* I concede, furrowing my brow and looking down as I swirl the coffee in the bottom of my glass while taking on an air of troubled superiority, *"but it still isn't private, and they WILL use it."* *"Well then we can just fly it in under the radar,"* you counter. *"We'll make it not be enumerable. Nobody will suspect it's there!"* ```js class Foo { constructor(x) { Object.defineProperty(this, 'x', { value: x, enumerable: false, }); } getX() { return this.x; } } ``` [Setting my glass down] *"That is, until they read the source code,"* I reply, deadpan. *"We'll keep it truly private, then,"* you chortle nervously, looking at others in the room for support, all of whom refuse to make eye contact. *"We'll just keep everything hidden inside the constructor's closure. Problem solved!"* ```js class Foo { constructor(x) { this.getX = () => x; } } ``` *"But now,"* I argue, raising my palm to my forehead, *"every instance of that class has a duplicate copy of that function. It doesn't even exist on the prototype!"* *"Okay then,"* you say, grasping at straws, *"we'll store it outside the class in a map where nobody can reach it, maybe?"* ```js const __ = new Map(); class Foo { constructor(x) { __.set(this, { x }); } getX() { var { x } = __.get(this); return x; } } ``` *"But now you have a memory leak,"* I refute triumphantly, smelling victory. *"That map keeps STRONG references to every class instance you put in it, thus retaining it in memory by creating a referential backpath to a GC root!"* *"Hmmm..."* you wonder, stroking your chin and narrowing your eyes. *"Then we'll just make it a WeakMap."* ```js const __ = new WeakMap(); class Foo { constructor(x) { __.set(this, { x }); } getX() { var { x } = __.get(this); return x; } } ``` Me: *sweats profusely*.