1 单例类singleton.js
var _gInstance = null;
function singleton(){
this.a = 0;
singleton.prototype = {
constructor:singleton,
setA:function(b){
this.a = b;
}
getA:function(){
return this.a;
}
this.getInstance = function(){
if(_gInstance == null){
_gInstance = new singleton();
}
return _gInstance;
}
}
module.exports = singleton;
2 调用
在任何需要的地方
var Singleton = require('./singleton');
var singleton = new Singleton();
var stInstance = singleton.getInstance();
stInstance.setA(1000);
var a = stInstance.getA();