I have the following master ES6 class that holds references to other ES6 classes that are dynamically called later in my code like so:
class Root {
constructor() {
this.refs = {
ref1: require('ref1')
ref2: require('ref2')
}
}
}
module.exports = exports = new Root;
ref1 and ref2 export new instance of the class Ref1
and class Ref2
.
Later down the line, I attempt to make an instance of one of these references classes by doing:
const Root = require('root');
class other {
someFunc() {
var ref = new Root.refs['ref1'](value);
}
}
This always ends with the following error: TypeError: this.actions[action.action] is not a constructor
How can I properly make a reference to these ref classes?
You are missing ()
after new Root
.
If the export is set with default you might also want to try with require('ref1').default
You are not exporting the class Root
you are exporting an instance of class Root by using the new
keyword.
Change it to say module.exports = Root;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.