Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda