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

76
Visualizações
Organising a large number of imported javascript functions

I'm creating an educational website with a lot of interactive exercises. Each exercise has a function which generates each question/answer combination. At the moment I am importing all of these from a file:

import * as exerciseHelpers from "../helpers/exercise_helpers.js";

Inside the helper file each exercise has its function:

export function exercise1() {
  //stuff for exercise 1
}

export function exercise2() {
  //stuff for exercise 2
}

I am worried this file will get very hard to maintain with 100s of exercises. Also I don't really want to load in all the functions just to call the one relating to the current exercise.

When I'm running the exercise I call:

   exerciseHelpers[thisExerciseFunction]();

Ideally I'd like to create each function in its own file exercise1.js/exercise2.js etc, and then just import the one I need.

But it seems I can't import functions based on a variable.

Is there a better way to organise this so each of these functions can be in a separate file that is loaded in when needed?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Create an index.js file in your helpers directory. Inside index.js export all your functions from their respective files, like:

export * from './exercise1.js';
export * from './exercise2.js';

Then you can have all your utility functions in their own files but still import everything from index.js:

import * as exerciseHelpers from '../helpers';

Or you can import the one you need with named import:

import { exercise1 } from '../helpers';
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