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

231
Vistas
Javscript class constructor using destructuring?

I've done this before, however I like to try and stick to mainstream practices when writing so that others can read it easily enough so I thought I'd ask if it's "bad" to de structure constructors with classes?

e.g:

// how you'd see a class wrote out normaly
class Task {
  constructor(label, assignedTo) {
    this.label = label;
    this.assignedTo = assignedTo;
  }
}

// with destructuring ????? 
class Task {
  constructor({label, assignedTo='unassigned'}) {
    this.label = label;
    this.assignedTo = assignedTo;
  }
}

But why?

Well as you can see I assigned an value to assignedTo so that it could be left blank and still have a value. For example if we looked at a Express JS route we could do something like...

// express js API preparing data before saving to db
 
// ...

// req.body content:
// { label: 'empty dishwasher' }

// assuming the whole request body has been validated by a middleware
// pass the whole body in where req.label is an optinal field
const task = new Task({ ...req.body });

// ...

This way I don't have to sit there an write req.body.xxx or const {label, assignedTo} = req.body; like this this:

// ...

const { label, assingedTo } = req.body;

// just an example of how I could still add a value if assingedTo is undeffined
const task = new Task(label, assingedTo ? assingedTo : 'unassinged');

// ...

I suppose this is "just another way" to do something however I was wondering if anyone would see this as bad practice? I personalty think it could be useful but I never see anyone doing it

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I see two good practices here. Passing single object param with props for different param values, instead of multiple params. And setting default value for params.

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