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 can try the "JavaScript" toolkit I wrote and randomly define the name.
https://github.com/masx200/custom-elements-random-define


```js
var mycom = class extends HTMLElement {};
const tag =RandomDefine(mycom);
var myele = new mycom();
```
```js
var mycom = class extends HTMLDivElement {};
const tag =RandomDefine(mycom, "div");
var myele = new mycom();
```

```ts
interface Class {
  new (): HTMLElement;
  prototype: HTMLElement;
}
function RandomDefine(initclass: Class, extendsname?: string): string;

```

-- 
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-539598348

Received on Tuesday, 8 October 2019 16:37:55 UTC