Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda