Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda