Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!