-
-
Save piyushchauhan2011/76dc89c7e6237c88da2b7dfbefc6f58c to your computer and use it in GitHub Desktop.
Revisions
-
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ class Foo { } ``` *"Please."* I respond, rolling my eyes. *"That `x` variable 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!"* -
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -60,7 +60,7 @@ class Foo { *"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, keyed by instances, where nobody can reach it, maybe?"* ```js const __ = new Map(); -
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -78,7 +78,7 @@ class Foo { *"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, well after the app has finished using it!"* *"Hmmm..."* you wonder, stroking your chin and narrowing your eyes. *"Then we'll just make it a [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)."* ```js const __ = new WeakMap(); -
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,7 +76,7 @@ class Foo { } ``` *"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, well after the app has finished using it!"* *"Hmmm..."* you wonder, stroking your chin and narrowing your eyes. *"Then we'll just make it a WeakMap."* -
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,7 +76,7 @@ class Foo { } ``` *"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."* -
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -60,7 +60,7 @@ class Foo { *"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(); -
greim revised this gist
Apr 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -48,7 +48,7 @@ class Foo { [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 { -
greim revised this gist
Apr 13, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ class Foo { } ``` *"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!"* @@ -28,7 +28,7 @@ class Foo { } ``` *"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!"* -
greim created this gist
Apr 13, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ # 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 everyone 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 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. *"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, glancing about nervously, *"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."* *"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*.