如何创建继承HTMLElement的类?html-element模块使用问题
正确实现方式
你不需要实例化HTML.HTMLElement,因为类继承的目标是类本身,而非类的实例。直接将HTML.HTMLElement作为父类即可:
let HTML = require('html-element'); class MyElement extends HTML.HTMLElement { #xValue = 0; constructor() { super(); this.onclick = this.#clicked.bind(this); } #clicked() { // 编写点击事件逻辑 this.#xValue++; console.log('点击次数:', this.#xValue); } }
补充说明
html-element模块导出的HTMLElement是构造函数(类),可直接用作继承的父类,无需用new创建实例。- 若需注册自定义元素(模拟浏览器自定义元素API),可使用模块提供的
define方法:
HTML.define('my-element', MyElement);
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

