Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda