function Resource() {
    // 如果不是第一次new(instance肯定是存在的)
    if (Resource.instance)
      return Resource.instance;
    else { // 否则(instance不存在)
      // 组装新对象
      this.balance = 100;

      // 将其存到Resource机器上
      Resource.instance = this;
    }
  }

  var r = new Resource();

  console.log('r:', r)
  r.balance = 50;
  console.log('r:', r)

  var r2 = new Resource();

  console.log('r2:', r2);

  r.balance = 55;

  console.log('r2:', r2);

dafeixiong
43 声望0 粉丝