Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

333
Visualizações
ReferenceError for defined variables

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda