I have javascript classes in multiple files. I would like to call a function from a given class by only supplying their names as arguments.
class Index{
constructor(){
this.Loader("test", "TestFunc");
}
Loader(cname, fname){
const template = require(`./${cname}.js`);
const object = new template();
object[fname].apply();
}
}
new Index();
module.exports = class Test{
constructor(){
this.text = "this text should appear";
}
TestFunc(){
console.log(this.text);
}
}
When the instance of the Test class gets initialized the constructor will run and prepare the value, but when I call TestFunc via the apply function the same values appear undefined and the error "Cannot read property text of undefined" gets thrown. When calling the function normally via object.TestFunc(); no such error is present.