I wrote the following code (mock up) :
module.exports = {
test() {
console.log('test')
},
iffer(nb) {
if (nb !== 0) {
this.test()
}
}
}
I got the error "test is not a function".
And tried the following :
let that = this
module.exports = {
test() {
console.log('test')
},
iffer(nb) {
if (nb !== 0) {
that.test()
}
}
}
This solution still doesn't works as JavaScript modules have strict mode on by default. Does anyone know a solution to this problem?
Just reference module.exports
again.
iffer(nb){
if (nb !== 0) {
module.exports.test()
}
}
You could also put the exported object into a standalone variable first to make it easier to reference. Or, have all functions be standalone for easy reference, then export an object with all of them.
const test = () => console.log('test');
const iffer = (nb) => {
if (nb !== 0) {
test();
}
};
module.exports = { test, iffer };
If your environment is compatible with ES6 module syntax, I'd recommend using them instead.
export const test = () => console.log('test');
export const iffer = (nb) => {
if (nb !== 0) {
test();
}
};