home.social

#privatefields — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #privatefields, aggregated by home.social.

  1. What is the difference between extending a subclass of an object and `Object.create`-ing based on a prototype for the purposes of JS private fields? I'm surprised to get different behavior between:

    ```javascript
    class Foo {
    #foo = 'foo';

    doSomething(): void {
    console.log(this.#foo);
    }
    }

    class Bar extends Foo {}

    new Foo().doSomething(); // Works
    new Bar().doSomething(); // Works
    Object.create(new Foo()).doSomething(); // ERROR: Cannot read private member.
    ```

    In my mind, they should create basically the same prototype inheritance. Why does `Object.create` fail?

    typescriptlang.org/play/?targe

    #JavaScript #PrivateFields

  2. What is the difference between extending a subclass of an object and `Object.create`-ing based on a prototype for the purposes of JS private fields? I'm surprised to get different behavior between:

    ```javascript
    class Foo {
    #foo = 'foo';

    doSomething(): void {
    console.log(this.#foo);
    }
    }

    class Bar extends Foo {}

    new Foo().doSomething(); // Works
    new Bar().doSomething(); // Works
    Object.create(new Foo()).doSomething(); // ERROR: Cannot read private member.
    ```

    In my mind, they should create basically the same prototype inheritance. Why does `Object.create` fail?

    typescriptlang.org/play/?targe

    #JavaScript #PrivateFields

  3. What is the difference between extending a subclass of an object and `Object.create`-ing based on a prototype for the purposes of JS private fields? I'm surprised to get different behavior between:

    ```javascript
    class Foo {
    #foo = 'foo';

    doSomething(): void {
    console.log(this.#foo);
    }
    }

    class Bar extends Foo {}

    new Foo().doSomething(); // Works
    new Bar().doSomething(); // Works
    Object.create(new Foo()).doSomething(); // ERROR: Cannot read private member.
    ```

    In my mind, they should create basically the same prototype inheritance. Why does `Object.create` fail?

    typescriptlang.org/play/?targe

    #JavaScript #PrivateFields

  4. What is the difference between extending a subclass of an object and `Object.create`-ing based on a prototype for the purposes of JS private fields? I'm surprised to get different behavior between:

    ```javascript
    class Foo {
    #foo = 'foo';

    doSomething(): void {
    console.log(this.#foo);
    }
    }

    class Bar extends Foo {}

    new Foo().doSomething(); // Works
    new Bar().doSomething(); // Works
    Object.create(new Foo()).doSomething(); // ERROR: Cannot read private member.
    ```

    In my mind, they should create basically the same prototype inheritance. Why does `Object.create` fail?

    typescriptlang.org/play/?targe

    #JavaScript #PrivateFields

  5. What is the difference between extending a subclass of an object and `Object.create`-ing based on a prototype for the purposes of JS private fields? I'm surprised to get different behavior between:

    ```javascript
    class Foo {
    = 'foo';

    doSomething(): void {
    console.log(this.);
    }
    }

    class Bar extends Foo {}

    new Foo().doSomething(); // Works
    new Bar().doSomething(); // Works
    Object.create(new Foo()).doSomething(); // ERROR: Cannot read private member.
    ```

    In my mind, they should create basically the same prototype inheritance. Why does `Object.create` fail?

    typescriptlang.org/play/?targe