// toast.jsclass Toast { construtor(code) { if (code !== 1000) { throw('请调用toast.init()生成实例') } this.timer this.instance this.create() } create() { this.ele = document.createElement('div') this.ele.classList.add('toast') } show(str, duration) { // 展示 if(timer) { this.hide() } this.ele.innerHTML = str this.ele.classList.add('show') this.timer = setTimeOut(()=> { this.hide() }) } hide() { // 隐藏 this.ele.innerHTML = '' this.ele.classList.remove('show') clearTimeout(this.timer) } static init() { return this.instance || (this.instance = new Toast(1000)) }}