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

173
Visualizações
Function default object, props with default values are undefined

I am working on a function and when I set all of the required inputs as object props and some of them with default values, those that I gave default values to are undefined.

Basically, I want to create some of the properties of the object required and others not.

The issue is that, if I provide a value to containerClassName for example, all of the props that have default values become undefined and I can't figure out why?

This will log all default values, no problems

beforeAfterComparison()

This will log undefined for all of them

beforeAfterComparison({ containerClassName: "someClassName" })

function beforeAfterComparison(
  {
    containerClassName,
    beforeImageUrl,
    afterImageUrl,
    containerIndex,
    lineWidth,
    lineColor,
    circleDiameter,
    circleColor,
  } = {
    containerIndex: 0,
    lineWidth: 2,
    lineColor: "black",
    circleDiameter: 50,
    circleColor: "lightgray",
  }
) {
  console.log(containerIndex);
  console.log(lineWidth);
  console.log(lineColor);
  console.log(circleDiameter);
  console.log(circleColor);
}

beforeAfterComparison();
console.log("-------------------");
beforeAfterComparison({ containerClassname: "someClassName" });

I am expecting that in both cases I should get my default values.

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

0

I think the syntax is wrong. Try this,

 function beforeAfterComparison(
  {
    containerClassName,
    beforeImageUrl,
    afterImageUrl,
    containerIndex = 0,
    lineWidth = 2,
    lineColor = "black",
    circleDiameter = 50,
    circleColor = "lightgray",
  } = {}) {
  console.log(containerIndex);
  console.log(lineWidth);
  console.log(lineColor);
  console.log(circleDiameter);
  console.log(circleColor);
}
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