Re: [WICG/webcomponents] [element-internals] How to get internals in base class and subclass, without leaking it to public (Issue #962)

This thread proposes a "private class field symbols" syntax idea, which would allow "protected" to be possible, and it also explains how this would be useful for features like `ElementInternals` which `proctected` would be super useful for:

https://es.discourse.group/t/private-class-field-symbols/2262

To summarize, it would allow people to define elements as follows, making being "open to extension" easier:

```js
const internals = Symbol()

export class BaseElement extends HTMLElement {
  #[internals]

  constructor() {
    super()
    this#[internals] = this.attachInternals()
  }
}
```

If the subclass was not already getting the internals, that is still problematic because the subclass will still be calling `attachInternals` and the base class, if it begins to call `attachInternals`, will start to cause a runtime error.

With this `BaseElement` example, now a subclass can openly extend by accessing the internals:

```js
import {internals, BaseElement} from 'some-lib'

class SomeElement extends BaseElement {
  method() {
    this#[internals] // use the internals
  }
}

customElements.define('some-el', MyEl) // "finalize" the class (subclasses of SomeElement will need a new name).
```

Public users will not be able to use the internals:

```js
import {internals} from './some-lib'

const el = document.querySelector('some-el')
el.method() // ok
el#[internals] // SyntaxError (good!)
```

The best solution would be, if `attachInternals` could be updated so that it can be called any number of times (not only once) when inside an custom element `constructor` call stack. Then subclasses could simply call it, even if a base class doesn't use it yet. Problems solved.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/962#issuecomment-2563178839
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/962/2563178839@github.com>

Received on Thursday, 26 December 2024 23:59:49 UTC