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.

Why are #private fields not source-positional? #328

Description

@trusktr

I.e. why are they not scoped at the module level instead of at the class level?

This means we can't use #private fields along with class-factory mixins.

This works fine:

class Foo {
    #foo = 123
    test(other) {
      console.log(other.#foo)
    }
  }
}

class Cat extends Foo {}
class Dog extends Foo {}

const c = new Cat
c.test(new Dog) // logs 123.

Now we want to make the Foo class mixable (very beneficial for code organization):

function Foo(Base) {
  return class Foo extends Base {
    #foo = 123
    test(other) {
      console.log(other.#foo)
    }
  }
}

class Cat extends Foo(Object) {}
class Dog extends Foo(Object) {}

const c = new Cat
c.test(new Dog) // ERROR, Cannot read private member #foo from an object whose class did not declare it

Typescript playground example (maybe TS team overlooked this, because there is no type error).

Activity

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

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