Can I set a private class field using a variable as identifier? How?

No, this doesn’t look to be possible. From the proposal FAQ:

Why doesn’t this[‘#x’] access the private field named #x, given that this.#x does?

  1. This would complicate property access semantics.

  2. Dynamic access to private fields is contrary to the notion of ‘private’. E.g. this is concerning:

class Dict extends null {
  #data = something_secret;
  add(key, value) {
    this[key] = value;
  }
  get(key) {
    return this[key];
  }
}
(new Dict).get('#data'); // returns something_secret

The syntax is such that every private field must be initialized and/or referenced with # before the literal property name, and nothing else. Not even bracket notation is permitted.

Having a private field named x must not prevent there from being a public field named x, so accessing a private field can’t just be a normal lookup.

You can’t even reference a private field unless it’s explicitly defined in the class body (not a class function, but inside the class body directly):

class Foo {
  // error is thrown because #abc must be defined in this section
  doSomething() {
    return this.#abc;
  }
}

That said, nothing’s stopping you from creating a private property which is an object, with all of those properties on the object:

class Foo {
  #privates = {};

  constructor(properties) {
    Object.entries(properties).forEach(
      ([name, value]) => (this.#privates[name] = value)
    );
  }

  get privates() {
    return this.#privates;
  }
}

const foo = new Foo({ bar: 2 });

console.log(foo.privates.bar);

Leave a Comment