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

126
Vistas
YDNJS: scope and closures hoisting wrong example

I am reading YDNJS: scope and closures, And in chapter 4 which talks about hoisting it says that

Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional as this code implies

foo(); // "b"
var a = true;
if (a) {
function foo() { console.log("a"); }
}
else {
function foo() { console.log("b"); }
}

the author assumes that foo() will print 3 to console as the last function declaration override the previous one in the if block before the engine evaluates the condition.

The problem is that when I run this code on my own it throws a TypeError: foo is not a function instead of 3.

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

0

Conditionally created functions Functions can be conditionally declared, that is, a function statement can be nested within an if statement, however the results are inconsistent across implementations and therefore this pattern should not be used in production code. For conditional function creation, use function expressions instead.

var hoisted = "foo" in this;
console.log(`'foo' name ${hoisted ? "is" : "is not"} hoisted. typeof foo is ${typeof foo}`);
if (false) {
  function foo(){ return 1; }
}

In Chrome: 'foo' name is hoisted. typeof foo is undefined

In Firefox: 'foo' name is hoisted. typeof foo is undefined

In Edge: 'foo' name is not hoisted. typeof foo is undefined

In Safari: 'foo' name is hoisted. typeof foo is function

Read more about it here 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