Making a simple ORM with ES6 classes, I run with a blocking issues – I can't properly copy a class (as I would have with util.extend in ES5).
Specifically, here is what I tried:
class BaseModel {
echo() {
console.log(this.props);
}
static _setProperties(props) {
this.props = props;
}
}
function makeModel(props) {
// Try to copy the Class object
const Model =
Object.assign(Object.create(Object.getPrototypeOf(BaseModel)), BaseModel);
// Copy my static methods – is there a better way to do this?
Object.getOwnPropertyNames(BaseModel).forEach((key) => {
if (!key.startsWith('_')) return;
Model[key] = BaseModel[key];
});
// Configure the new model
Model._setProperties(props);
return Model;
}
const GreeterModel = makeModel('hello');
const greeter = new GreeterModel();
greeter.echo(); // Should log hello
The error I get is:
TypeError: GreeterModel is not a constructor
Is there any way to achieve this with ES6 classes, or do I have to stick to ES5-style?
Optional question: is there a better way to copy the static methods? The solution with getOwnPropertyNames is not ideal as it returns also read-only properties, such as length.
Thanks!
Object.createdoes not create functions, this code wouldn't work with ES5 either.class extends?