Green's Blog

导航

 

单例模式

 

function Universe() {
  //缓存实例
  var instance = this;

  this.start_time = 0;
  this.bang = 'Big';

  //重写改构造函数
  Universe = function() {
    return instance;
  };
}

Universe.prototype.nothing = true;

// 测试
var uni = new Universe();
var uni2 = new Universe();
console.log(uni === uni2); // true

console.log(uni.nothing) // true
console.log(uni2.nothing) // true

Universe.prototype.everything = true;

console.log(uni.everything) // undefined
console.log(uni2.everything) // undefined

  

posted on 2015-04-14 10:42  GreenBlog  阅读(127)  评论(0编辑  收藏  举报