SOURCE

// 使用weakMap的方式实现私有成员变量
/**
 * 创建weakMap对象
 *
 * @class CreateWeakMap
 */
class CreateWeakMap {
  constructor(self) {
      this.self = self;
      this.data = new WeakMap();
  }
  get(key) {
      return this.data.get(key || this.self) || {};
  }
  set(key, value) {
      if (typeof value === 'object' && !(value instanceof Array)) {
          let data = this.data.get(key) || {};
          Object.assign(data, value);
          this.data.set(key || this.self, data);
      } else {
          this.data.set(key || this.self, value);
      }
  }
}

let _data = new CreateWeakMap();

class test1 {
  get name() {
    return _data.get(this).name
  }
  set name(name) {
    _data.set(this, {
      name
    });
  }
}

console 命令行工具 X clear

                    
>
console