I'm baffled. I imagine I've tread on some bit of JavaScript esoterica but I don't know what it is.
The code is
processMessage(ws, json){
let msg = JSON.parse(json);
console.log(`Message(${msg.type}): ${msg.data}`);
console.log("Available games: " + msg.data); // Normally isn't here, put it in to make sure I wasn't going crazy
switch (msg.type){
case "welcome":
console.log("Welcome received. Requesting available games.");
let msg = {
"type":"game list",
"data":null
};
ws.send(JSON.stringify(msg));
break;
case "restore":
break;
case "game list":
console.log("Available games: " + msg.data);
break;
case "create console":
break;
}
}
It generates this output
Message(welcome): undefined MainUI.js:94:21
Available games: undefined MainUI.js:95:21
Welcome received. Requesting available games. MainUI.js:98:29
Message(game list): The Office MainUI.js:94:21
Available games: The Office MainUI.js:95:21
Uncaught ReferenceError: can't access lexical declaration 'msg' before initialization MainUI.js:109:21
processMessage http://localhost:8000/MainUI.js:109
onmessage http://localhost:8000/MainUI.js:44
loaded http://localhost:8000/MainUI.js:42
onload http://localhost:8000/:1
I'm pretty sure that output has msg, or its properties, being accessed 1,2,3...4 times before the error occurs.
Aaaaaand, I just found the cause. Knew it was my fault but there's the esoterica.
Within the switch itself, I put
case "welcome":
console.log("Welcome received. Requesting available games.");
let msg = {
"type":"game list",
"data":null
};
ws.send(JSON.stringify(msg));
break;
That second let msg, instead of providing an error like 'msg' has already been declared, somehow confuses the interpreter into thinking that msg hadn't yet been declared. Removing let and just having msg = makes the error go away.
If anyone reads this and has a reference to explain the reason this happens, I'd really appreciate your adding a comment with a link.