变量同时真实和错误

时间:2017-05-05 19:48:31

标签: javascript ecmascript-6 babel

这是我的代码

class ToolDispatcherBlueprint extends Destroyable {
  _tool;
  _dispatchRegister = null;

  _state = {
    selectedTool: null,
    selectedElement: null,
    hoveredElement: null,
    keyboardKeyDown: null,
    keyboardKeyUp: null,
    mousedown: false,
    mouseup: true,
    mousemove: false
  };

  constructor(paper) {
    super();
    this._tool = new paper.Tool;

    let dispatchOnKeyDown = (event) => {
      this._state.keyboardKeyDown = event;
      this.dispatch(event);
    };

    let dispatchOnKeyUp = (event) => {
      this._state.keyboardKeyUp = event;
      this.dispatch(event);
    };

    let dispatchOnMouseDown = (event) => {
      this._state.mouseup = false;
      this._state.mousemove = false;
      this._state.mousedown = true;
      console.log("dispatcher", this._state.mousedown, this._state);
      this.dispatch(event);
 ...

问题是我得到一个日志,表明mousedown同时为true和false。我希望它表示true,_state的属性也表示true即。 _state.mousedown: true

enter image description here

2 个答案:

答案 0 :(得分:2)

它不是你的想法。打印时,true会通过。但该对象由开发工具引用。它仅在您展开对象时进行评估,到那时,您的mousedown事件已结束。

答案 1 :(得分:0)

值肯定不会同时变化。 console.log(this._state)从稍后输出对象的一些快照。

永远不要依赖于对象在控制台中的显示方式。只能信任原始值。如果需要输出对象的快照,请逐个输出其属性或将其转换为基元:

console.log(JSON.stringify(this._state, null, 2))
相关问题