首页 > 代码库 > js singleton

js singleton

var singleton = (function(){
    var instance;

    return function () {
        if (instance) return instance;
        instance = this;
        return instance;
    }
}());

var obj = new singleton();
var obj2 = new singleton();
console.log(obj2 === obj);  // true

  

js singleton