我在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);
}
});
}
}