I have used the following function to create instances of unknown classes for some time:
Kernel.prototype._construct = function (constr, args) {
function F() {
constr.apply(this, args); // EXCEPTION!
}
F.prototype = constr.prototype;
return new F();
};
If I use prototypes everything works:
function Person(name, surname) {
this.name = name;
this.surname = surname;
}
var person = Kernel._construct(Person, ["name", "surname"]); // WORKS!
However, some people are using my library using ES6 native classes in node v4+:
class Person {
constructor(name, surname) {
this.name = name;
this.surname = surname;
}
}
var person = Kernel._construct(Person, ["name", surname]); // EXCEPTION!
They are getting an error:
TypeError: Class constructors cannot be invoked without 'new'
I need to be able to invoke the constructor with an unknown number of arguments. Any ideas about how to get around this issue?