// 单例模式就是共享一块内存,new 一个实例不会再开辟新内存
class Modal {
constructor() {
this.dom = null;
if (!Modal.instance) {
this.dom = document.createElement("div");
this.dom.innerHTML = "this is a modal!"
this.dom.id = 'modal';
this.dom.style.display = "none";
this.dom.show = this.show.bind(this);
this.dom.hide = this.hide.bind(this);
document.body.appendChild(this.dom);
Modal.instance = this.dom;
}
return Modal.instance;
}
show() {
this.dom.style.display = 'block';
}
hide() {
this.dom.style.display = 'none';
}
}
document.getElementById("open").addEventListener("click", function () {
let modal = new Modal();
modal.show();
})
document.getElementById("close").addEventListener("click", function () {
let modal = new Modal();
modal.hide();
})
console