Re: [w3c/webcomponents] Anonymous custom elements 匿名自定义组件 (#842)

In a recent [post](https://component.kitchen/blog/posts/supporting-both-automatic-and-manual-registration-of-custom-elements) about custom element registration, I expressed support for an idea along these lines. Like @masx200, we've had to invent a means to auto-register component classes with generated names. As that post indicates, we almost never use registered component tags in our code — we create all components via their constructors, so registration of a tag for each class is undesirable overhead for us.

It doesn't seem feasible (or even desirable) to actually remove the need for custom element tags, but I wonder whether it would be possible to have invocation of a component constructor trigger auto-registration of that class using a generated tag name if the class isn't already registered. E.g.,

```js
// Can instantiate without explicit registration.
class MyElement extends HTMLElement {}
const myElement = new MyElement(); // works, implicitly registers class

// Can reference localName of implicitly-registered class
const tag = myElement.localName; // "my-element-a87f3ee9" or something

// The generated tag is usable.
const myElement2 = document.createElement(tag);

// Once implicitly registered, a class can't be explicitly registered.
customElements.define('my-element', MyElement); // throws
```


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

Received on Tuesday, 8 October 2019 16:29:48 UTC