Skip to content

Alternative: private class field symbols #13

Description

@trusktr

https://es.discourse.group/t/private-class-field-symbols/2262

The advantage over this proposal is that you have more control:

  • if we do not export the symbol from a module, then we effectively have a private key for any classes in the same file, which is basically this proposal (except that the syntax of "private class field symbols" is available only in lexical scope of a class).
  • if we export the symbol from a module, then we effectively have "protected" because any class that extends a base class can also use the symbol to access the same state (but the state is still not public because obj#[foo] would be a syntax error outside of a class

The syntax may be debated, f.e. this#[foo] or this.#[foo] or this[#foo], but I like this approach more because as the author of the Symbol, I would be able to make a choice: keep all the classes in the same file and not export the symbol (they are all "friend" classes then), or export the symbol as well as the classes (subclasses have protected access now).

In particular, a protected feature is needed for some real-world use cases, as described in that es.discourse thread with Custom Elements and ElementInternals. The ElementInternals API was designed in such a way that a class author could guarantee that the internals ould be private, but making them protected (which many people want) is very cumbersome.

So we need something that we can control, and the current private #var syntax seems much too restricted (to a single file).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions