December 31st, 2020

(no subject)

I am very confused by the javascript private fields syntax.

In typescript, it's sort of intuitive:

class Parent {

  private foo: string;
  protected bar: string;

  constructor() { = 'foo'; = 'bar';

  doStuff() {
    return `${} ${}`;

class Child extends Parent {

  // does not inherit foo from parent
  // but does inherit bar from parent

  doStuff() {


I say "sort of" because, with private fields available only to the instances of the same class and not to the subclasses, the Child class in the example above will inherit the constructor from the parent, in which it will try to assign 'foo' to, which it isn't supposed to have. I don't know how other languages handle this — they probably ignore it.

In Javascript, however, according to the proposal that is bound to be included in the language, you'll have to prepend an ugly octothorpe to private field names, and then always use the said octothorpe when referencing these fields. Your class will be allowed to have both (public field) and
(private field). And I can't quite tease out from the proposal whether the subclasses will have access to the private/protected fields of the parents. It looks like they won't. In which case I can't understand why people don't see it as a problem.