Skip to content

Instantly share code, notes, and snippets.

@piyushchauhan2011
Forked from greim/es6-private-members.md
Created April 13, 2016 08:23
Show Gist options
  • Select an option

  • Save piyushchauhan2011/76dc89c7e6237c88da2b7dfbefc6f58c to your computer and use it in GitHub Desktop.

Select an option

Save piyushchauhan2011/76dc89c7e6237c88da2b7dfbefc6f58c to your computer and use it in GitHub Desktop.

Revisions

  1. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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!"*
    *"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!"*

  2. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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?"*
    *"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();
  3. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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."*
    *"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();
  4. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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!"*
    *"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."*

  5. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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."*
    *"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."*

  6. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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, glancing about nervously, *"we'll store it outside the class in a map where nobody can reach it, maybe?"*
    *"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();
  7. @greim greim revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion es6-private-members.md
    Original 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. *"We'll just keep everything hidden inside the constructor's closure. Problem solved!"*
    *"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 {
  8. @greim greim revised this gist Apr 13, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions es6-private-members.md
    Original 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 everyone to read and write!"*
    *"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 superiority, *"but it still isn't private, and they WILL use it."*
    *"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!"*

  9. @greim greim created this gist Apr 13, 2016.
    97 changes: 97 additions & 0 deletions es6-private-members.md
    Original 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*.