Typescript global variables becoming undefined after function call

后端 未结 1 1870
眼角桃花
眼角桃花 2021-01-24 00:11

In my code, I have two global variables defined as

constructor() {
        this.map = new Map();
        this.player = new Player([], \"\");
    }
相关标签:
1条回答
  • 2021-01-24 00:42

    Your this is most likely referring to another object depending on how handleInput is being called. In your contructor(), either bind handleInput to this or change your handleInput to use arrow function:

    constructor() {
      this.handleInput = this.handleInput.bind(this);
    }
    

    Or:

    handleInput = (cmd:Command, arg:string):boolean => {}
    
    0 讨论(0)
提交回复
热议问题