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

252
Visualizações
Does if(this) {} else {} ever visit the else in JavaScript?

Is there any case where the condition if(this) {} fails? Because I couldn't think of a scenario where this is undefined.
This is not an important question nor something anyone would normally use, I'm just asking out of pure curiosity.

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

0

Yes, in some execution contexts, this is undefined, which is falsy in JavaScript:

In strict mode, however, if the value of this is not set when entering an execution context, it remains as undefined, as shown in the following example:

Here is at least one convoluted scenario in which the following conditions must be met:

  • global or function execution in strict mode
  • the function was defined at the top-level (effectively being a function of window)
  • that function was called directly without invoking its parent (i.e. foo() and not window.foo())
  • the function returned this
function foo() {
  'use strict'; // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
  return this;
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

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