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

124
Visualizações
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 Respostas
Responde à pergunta

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 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