Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Forked from rauschma/_private-fields-faq.md
Last active February 20, 2018 21:54
Show Gist options
  • Select an option

  • Save jridgewell/c59e6ff1a3f7700ec26b0ca322643ea2 to your computer and use it in GitHub Desktop.

Select an option

Save jridgewell/c59e6ff1a3f7700ec26b0ca322643ea2 to your computer and use it in GitHub Desktop.

Revisions

  1. jridgewell revised this gist Feb 20, 2018. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions private-fields-object.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    function create() {
    return {
    private #data: '',
    add(str) {
    this.#data += str;
    },
    toString() {
    return this.#data;
    },
    };
    }
  2. @rauschma rauschma revised this gist Feb 13, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions _private-fields-faq.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    # Fields with arbitrarily scoped privacy

    Info on private fields: http://2ality.com/2017/07/class-fields.html

    ## Q&A

    * Why the prefix `#`?
    * You define keys and these keys additionally work completely differently from property keys.
    * Is the keyword `private` necessary?
  3. @rauschma rauschma revised this gist Feb 13, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions friend-fields-oop.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    // Can only be used within this scope
    private #data;

    class StringBuilder {
    // Accessible within this scope
    #data = '';
    add(str) {
    this.#data += str;
  4. @rauschma rauschma revised this gist Feb 13, 2018. No changes.
  5. @rauschma rauschma revised this gist Feb 13, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions _private-fields-faq.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Fields with arbitrarily scoped privacy

    * Why the prefix `#`?
    * You define keys and these keys additionally work completely differently from property keys.
    * Is the keyword `private` necessary?
  6. @rauschma rauschma revised this gist Feb 13, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _private-fields-faq.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    * Why the prefix `#`?
    * You define keys and these keys additionally work completely differently from property keys.
    * Is the keyword `private` necessary?
    * It could be omitted, but I like the distinction between declaration and mention.
    * It could be omitted (first mention declares), but I like the distinction between declaration and mention.
  7. @rauschma rauschma renamed this gist Feb 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @rauschma rauschma created this gist Feb 13, 2018.
    12 changes: 12 additions & 0 deletions friend-fields-oop.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    private #data;

    class StringBuilder {
    #data = '';
    add(str) {
    this.#data += str;
    }
    }

    function toString(sb) {
    return this.#data;
    }
    4 changes: 4 additions & 0 deletions private-fields-faq.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    * Why the prefix `#`?
    * You define keys and these keys additionally work completely differently from property keys.
    * Is the keyword `private` necessary?
    * It could be omitted, but I like the distinction between declaration and mention.
    15 changes: 15 additions & 0 deletions private-fields-fp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // Can only be used within this scope
    private #data;

    function create() {
    return {
    // Only accessible within this scope
    #data: '',
    };
    }
    function add(sb, str) {
    sb.#data += str;
    }
    function toString(sb) {
    return sb.#data;
    }
    9 changes: 9 additions & 0 deletions private-fields-oop.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class StringBuilder {
    private #data = '';
    add(str) {
    this.#data += str;
    }
    toString() {
    return this.#data;
    }
    }