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

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

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 Relatório

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 Relatório

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 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