Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
using this in JavaScript module

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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();
    }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!