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

264
Vistas
Calling a function recursively in the context of "export default"?

This may be obvious to others but I don't know the syntax for calling a function recursively in the following code:

//request.js
    export default {

      send() {
       ... do stuff ...
       this.send(); // Can't call "send" of undefined
      }
    }

//main.js
import request from './request'

export default class Main extends Component {
   init() {
      request.send();
  }
}

In the above example, I'm calling the send function from main.js. All this is working fine but I have a condition in the send function that will recall the same function. The problem is that I have no idea how to call the function from within the function. Anybody?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

If you want to call the default export recursively in Node.js, using Babel.js to transpile, you can use the exports object:

// request.js

export default {
  send() {
    console.log('Yo');
    setTimeout(() => {
      exports.default.send();
    }, 1000);
  },
};

Importing:

// main.js

import request from './request';

request.send(); // Writes "Yo" to the console once per second

This is because Babel.js simply transforms to the CommonJS syntax. This:

export default 42;

Becomes this:

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = 42;

No idea how this may change in a future implementation of ES6 Modules in Node.js.

over 4 years ago · Santiago Trujillo Denunciar

0

I don't see any reason why this.send() wouldn't work, given you are exporting an object with a method.

But if it doesn't work, you can always use a named function expression:

export default {
  send: function send() {
    … send() …
  }
};

or a named function declaration:

function send() {
  … send() …
}
export default { send };

or just name your exported object:

export default const request = {
  send() {
    … request.send() …
  }
};

But I would recommend to not default-export an object at all, and use named exports instead so that you can import them separately or on a namespace object:

// request.js
export default function send() {
  … send() …
}

// main.js
import * as request from './request'
…
request.send()
over 4 years ago · Santiago Trujillo Denunciar

0

well just as a quick glimpse, I would say that you should

 console.log(this)

to see the value of it in the first and second request. I didn't build your code to check it, but I assume it is an issue with your context.

Still not sure what you try to achieve, but you can always use

 someFunction.bind(this)

again it is quite vague but maybe it will give you an idea.

over 4 years ago · Santiago Trujillo 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