I'm currently needing to pass a class through a variable to a function which dynamically calls their constructor and so on. Short example:
class MyClass {
constructor ($q) {
console.log($q);
}
expose () {
return {};
}
}
const myclass = function () {
// My concern is this ------.
// I need to pass it |
// as a variable \|/
// '
let obj = Object.create(MyClass.prototype);
obj.constructor.apply(obj, arguments);
return {}; // other stuff goes here
};
myclass.$inject = ['$q'];
export {myclass};
My idea is to pass MyClass as a variable, to have it this way:
const myclass = function (classVariable) {
let obj = Object.create(classVariable.prototype);
obj.constructor.apply(obj, arguments);
return {}; // other stuff goes here
};
So that I can call it like let newclass = myclass(MyClass);
. This way if I have different classes (MyClass2, MyClass3, and so on) I don't have to repeat code in each file. If I try to do it this way though, Chrome throws an error saying that it cannot use a class as variable or something like that.
So, what would be the best approach to do this and avoid repeating code? it's actually a redesign I'm working on to avoid the copy-paste disaster made by someone else I need to fix. Thanks in advance!