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
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.