The Hottest Porn Videos Online mecum.porn Quality porns videos Free indian porn tube videos indiansexmovies.mobi hot indian women watch online
JS 设计模式

单例模式

前端面试指南:作用域和闭包

单例模式避免了重复实例化带来的内存开销。

// 单例模式
function Singleton() {
  this.data = "singleton";
}

Singleton.getInstance = (function () {
  var instance;

  return function () {
    if (instance) {
      return instance;
    } else {
      instance = new Singleton();
      return instance;
    }
  };
})();

var sa = Singleton.getInstance();
var sb = Singleton.getInstance();
console.log(sa === sb); // true
console.log(sa.data); // 'singleton'

References

  1. Learning JavaScript Design Patterns

本文链接:JS 设计模式

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:PorYoung,谢谢!^^

Previous
Next