首页 > Web开发 > 详细

js 单例模式

时间:2020-03-13 13:59:54      阅读:65      评论:0      收藏:0      [点我收藏+]

使用静态属性保存第一次示例对象,在以后的实例中,都将返回这个。

class Ajanuw {
  static __self?: Ajanuw;
  constructor(public name: string) {
    return this.__Ajanuw();
  }

  private __Ajanuw() {
    if (!Ajanuw.__self) {
      Ajanuw.__self = this;
    }
    return Ajanuw.__self;
  }
}

let a = new Ajanuw("ajanuw");
console.log(a.name); // ajanuw

let b = new Ajanuw("bbb");
console.log(a.name); // ajanuw

js 单例模式

原文:https://www.cnblogs.com/ajanuw/p/12485632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!