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

133
Visualizações
Preventing the defaults of unset object properties from being overridden in a JS function

So, I have a function with an object as a parameter:

function example(options = { param1: true, param2: 42 })
{
   return options.param2;
}

The problem is, if I call the function with parameters like this:

example({ param1: false });

the default for options.param2 gets overridden and set to undefined, even though it wasn't defined when the function was called. I realize why this is (the object is all one parameter so the entire thing gets set when you call the function), but is there any easy way to prevent this from happening, short of making all of the object parameters their own arguments in the function?

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

0

Destructure the argument instead, and assign default destructured values.

function example({ param1 = true, param2 = 42 } = {}) {
   return param2;
}
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