Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Support for privates in object literals? #318

@dgkimpton

Description

@dgkimpton

I was writing a simple object today and I wondered why private fields are restricted to classes?

e.g.

function make(name, ssnP) {
   return {
      name,
      validateUser(expectedSSN) {
         return ssnP === expectedSSN;
      },
  };
}

This works by closing over the ssn, but it seems that using the private-fields syntax we could make it much more explicit and not require any closure to be created.

function make(name, ssnP) {
   return {
      name,
      #ssn: ssnP,
      validateUser(expectedSSN) {
         return this.#ssn === expectedSSN;
      },
  };
}

However, if I understand the proposal correctly, this wouldn't be allowed because the object isn't defined using 'class'.

I'm curious why this is?

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