I'm looking into creating dynamic objects in Javascript. The way I'd like this to work is similar to new self();
in PHP. Solutions like down here unfortunately didn't solve the problem.
function factory(class_) {
return new class_();
}
The code sample given is causing me trouble.
import Model from './Model';
var className = 'course';
class Course extends Model {
save(success) {
API.post(className, this.data(), success, function() {
// notify the user if something went wrong.
});
}
static all(success, failure) {
API.get(className, (objects) => {
let all = [];
for(let object in objects) {
let newObject = new Course(objects[object]);
all.push(newObject);
}
success(all);
}, failure);
}
}
export default Course;
So to be clear, I'd like Course in let newObject = new Course(objects[object]);
to be dynamically assigned.
Instead of typing new Course(); I would like to do something like:
var className = 'Course';
let newObject = new className(objects[object]);
Naturally, this doesn't work. But how could I get it to work?
Thanks in advance!