在 中注册事件侦听器时attachedCallback
,我是否有责任确保在 中再次删除这些事件侦听器detachedCallback
?
如下面的最小示例所示,该模式是相当可预测的,所以我想知道浏览器是否已经处理了这个问题?
<my-element>0</my-element>
class MyElement extends HTMLElement {
createdCallback() {
this.update = this.update.bind(this);
}
attachedCallback() {
this.addEventListener("click", this.update);
}
detachedCallback() {
this.removeEventListener("click", this.update);
}
update() {
this.textContent = Math.random();
}
}
document.registerElement("my-element", {
prototype: MyElement.prototype
});