1 // 例2 === 模拟登陆框
 2   class LoginForm {
 3     constructor() {
 4       this.state = 'hide' // 存储当前显示,隐藏状态
 5     }
 6     show() {
 7       if (this.state === 'show') {
 8         alert('已经显示')
 9         return
10       }
11       this.state = 'show'
12       console.log('登陆框显示')
13     }
14     hide() {
15       if (this.state === 'hide') {
16         alert('已经隐藏')
17         return
18       }
19       this.state = 'hide'
20       console.log('登陆框已隐藏')
21     }
22   }
23   // 单例模式开启=>
24   LoginForm.getInstance = (function() {
25     let instance  // 寄存new出来的LoginForm实例 确保唯一性
26     return function() {
27       if (!instance) {  // 判断instance是否有值
28         instance = new LoginForm()
29       }
30       return instance
31     }
32   })()
33 
34   window.p1 = LoginForm.getInstance()

相关文章:

  • 2022-12-23
  • 2021-09-21
  • 2021-06-12
  • 2021-12-18
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
相关资源
相似解决方案