class MyClass {
constructor({title = 'wololo', status = 1}) {
this.title = title;
this.status = status;
// ... больше однотипных строчек
}
}
const defaults = {
title: 'wololo',
status: 1
};
const keys = Object.getOwnPropertyNames(defaults);
const validators = {
status(value) {
return !isNaN(value) && isFinite(value);
}
};
const transforms = {
status(value) {
return parseInt(value);
}
};
class MyClass {
constructor(props) {
for(let key of keys) {
if(!Object.prototype.hasOwnProperty.call(props, key)) {
this[key] = defaults[key];
continue;
}
if(typeof validators[key] === 'function' && !validators[key](props[key])) {
this[key] = defaults[key];
continue;
}
this[key] = typeof transforms[key] === 'function' ? transforms[key](props[key]) : props[key];
}
}
}