关于WeakMap和私有变量的问题

时间:2019-10-13 11:47:57

标签: javascript ecmascript-6 weakmap

在我目前正在阅读的书中,它讨论了如何使用WeakMap通过以下示例代码来加强隐私。

const Car = (function() {
  const carProps = new WeakMap();
  class Car {
    constructor(make, model) {
      this.make = make;
      this.model = model;
      this._userGears = ["P", "N", "R", "D"];
      carProps.set(this, { userGear: this._userGears[0] });
    }
    get userGear() {
      return carProps.get(this).userGear;
    }
    set userGear(value) {
      if (this._userGears.indexOf(value) < 0)
        throw new Error(`Invalid gear: ${value}`);
      carProps.get(this).userGear = value;
    }
    shift(gear) {
      this.userGear = gear;
    }
  }
  return Car;
})();

我不明白这样的代码如何真正地使齿轮属性私有化,并且不允许外部访问。

似乎通过使用

carProps.set(this, { userGear: this._userGears[0] });

我们隐藏了userGear并将其设为私有,因此无法对其进行访问。

但是,当我使用

const car1 = new Car("Toyota", "Prius");
console.log(car1);
console.log(car1.userGear);

它向我显示了

的结果
Car {
  make: 'Toyota',
  model: 'Prius',
  _userGears: [ 'P', 'N', 'R', 'D' ] }
P

我不确定为什么我可以访问userGear并在假定无法访问的地方得到'P'而不是'undefined'。

可能是我做错了或对这个概念的理解不正确。

有人可以帮助我了解WeakMap吗?

1 个答案:

答案 0 :(得分:2)

代码中显示的userGear的getter和setter只是用来向您展示如何在类内部的(私有)carProps与外部范围之间进行通信。该示例的目的是表明,除非通过故意公开的carProps方法,否则无法访问userGear变量。如果这些方法不存在,则在构造函数中设置WeakMap后,Car的外部使用者将无法查看或对其执行任何操作,例如:

const Car = (function() {
  const carProps = new WeakMap();
  class Car {
    constructor(make, model) {
      this.make = make;
      this.model = model;
      this._userGears = ["P", "N", "R", "D"];
      carProps.set(this, { userGear: this._userGears[0] });
    }
    shift(gear) {
      this.userGear = gear;
    }
  }
  return Car;
})();

const car = new Car('foo', 'bar');
// at this point, at this level of scope,
// there is no way for a user of "car" or "Car" to reference carProps
console.log(car.userGear);

对于另一个可能更有意义的示例,假设构造函数选择了一个类的用户必须猜测的随机数:

const Game = (function() {
  const gameProps = new WeakMap();
  return class Game {
    constructor() {
      gameProps.set(this, { randomNum: Math.floor(Math.random() * 10) });
    }
    guess(num) {
      return gameProps.get(this).randomNum === num ? 'Win' : 'Lose';
    }
  }
})();

const game = new Game();
// at this point, at this level of scope,
// there is no way for a user of "Game" or "game" to reference gameProps
// or to figure out the random number, without guessing multiple times
console.log(
  game.guess(1),
  game.guess(2),
  game.guess(3),
  game.guess(4),
  game.guess(5)
);

使用上述代码,Game的调用者无法数次不调用(故意公开的方法).guess来计算游戏的内部随机数。 (除非Math.random事先被猴子修补了...)