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

178
Visualizações
Should I use let or var when implementing a singleton?

I understand the difference between both (let locks the reference inside the block, whereas var declares a variable accessible scope-wide).

But considering the singleton pattern module-based:

var singleton = null;

module.exports = () => singleton ? singleton : singleton = newInstance();

Should I declare the singleton variable with let or var ? Is there any difference, considering CommonJS module imports/exports?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There is no difference between let or var, when we are talking about singleton implementation. I have already checked it in my IDE. Considering that using "let" is better practice, I suggest you to use exactly "let" keyword.

about 4 years ago · Juan Pablo Isaza Relatório

0

In case to choose between 'var' or 'let' the best choice is 'let' because one of the differences it that 'let' removes the error-prone behavior with variable hoisting, you can read this for specifications: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let. But you should consider using only const as your default variables because the majority of bugs out there involve unexpected state changes, everything you can use to guarantee variable values and states should be used to avoid this kind of most commum problems.

about 4 years ago · Juan Pablo Isaza Relatório

0

You've already consider to use const instead?

I've always used const and never had a problem, because you'll never reassing the entire object.

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