Skip to content

Instantly share code, notes, and snippets.

@joliss
Last active November 11, 2015 23:06
Show Gist options
  • Save joliss/6e5f568c18efff800585 to your computer and use it in GitHub Desktop.
Save joliss/6e5f568c18efff800585 to your computer and use it in GitHub Desktop.

Re https://github.com/wycats/javascript-private-state

  1. Re "Friend" access: From a theoretical point of view, it seems . But the strawman friend syntax introduces a lot of extra syntax.

    I wonder if we might use the same syntax for friends and non-friends, to keep things simpler. Along the lines of this:

    const data = Symbol();
    
    class MyClass {
      private #[data]; // instead of #data
      constructor (v) {
        this.#[data] = v;
      }
    }
    
    class Friend {
      static reportOnMyClass(m) {
        // Access m.#[data]. No extra "friend" syntax required.
      }
    }
    
    class OtherClass {
      private #[data]; // probably Error: can't reuse symbol
    }

    Requiring an explicit Symbol isn't quite as convenient, but this is a pretty advanced feature anyway, so it might be OK.

  2. I think we're all bothered by the fact that we're initializing private state twice: First automatically to undefined, then as an assignment in the constructor.

    Maybe we need something to the effect of initializer lists in C++?

    The proposal says: "Constructors need to [= must?] explicitly initialize slots to other values." I presume this is intended to address this issue, but I doubt that it's enough: What happens if you read from this.#private before it's initialized, either directly or by passing this to a friend? What happens if the assignment is inside an if?

  3. It seems that this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment