JavaScript TypeError:无法设置未定义的属性“未定义”

时间:2020-01-10 16:41:12

标签: javascript properties undefined typeerror

我在Javascript TypeError: Cannot set property 'undefined' of undefined上遇到了错误this.lastLogs[this.lastLogsIndex] = data;,但是控制台记录未定义任何响应:RESPONSE: LOG, map。我的数组也被定义:

class Client {
  constructor(_socket, _index, _key) {
    console.log('New Client created');
    this.browserSocket;
    this.key = _key;
    this.lastImage = null;
    this.lastLogs = new Array();
    this.lastLogsIndex = 0;

    this.socket = _socket;
    this.clientIndex = _index;

    this.socket.on('response', function(id, data) {
      console.log("RESPONSE: " + id + ", " + data);
      if (id == "IMG") {
        this.lastImage = data;
        this.browserSocket.emit('response', "IMG", this.getLastImage());
      } else if (id == "LOG") {
        this.lastLogs[this.lastLogsIndex] = data;
        this.lastLogsIndex++;
        this.browserSocket.emit('response', "LOG", data);
      } else {
        console.log("Invalid Id: " + id);
      }
    });
  }
}

0 个答案:

没有答案