How to inherit from the DOM element class

后端 未结 5 479
[愿得一人]
[愿得一人] 2020-11-30 07:59

I want to write some Javascript classes which extend DOM nodes (so that I can then insert instances of my class directly into the DOM), but am having difficulty finding out

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 08:49

    In 2020, you can easily create a custom element by extending HTML elements.

    class AppDrawer extends HTMLElement {...}
    window.customElements.define('app-drawer', AppDrawer);
    
    // Or use an anonymous class if you don't want a named constructor in current scope.
    window.customElements.define('app-drawer', class extends HTMLElement {...});
    

    More info and reference: Custom Elements

提交回复
热议问题