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

217
Vistas
Why can importing a file let some parts of that file be accessed but not things which aren't exported?

I was following a React tutorial where a javascript file A containing some functions and various console log statements etc is imported into another javascript file B.

One of those functions from file A is then called in file B but there was an error, yet all the console log statements and other commands from file A executed fine.

I wondered why some parts of the file A ran ok but not the functions, doesn't importing the whole file essentially bring everything in that file into scope for file B?

For instance:

a.js

console.log("hi");

function test1() {
  console.log("test1");
}

b.js

import './a.js';

test1();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

...doesn't importing the whole file essentially bring everything in that file into scope for file B?

No, not at all. The example module you've shown doesn't export anything, so everything inside it (the test1 function) is entirely private to the module. This is one of the key benefits of modules: They provide private scope for module details that you don't want to share, with an explicit way to share the things you do want to share.

I wondered why some parts of the file A ran ok but not the functions...

That's because you've imported the module. Even if the module doesn't import or export anything, its top-level code is still run as part of the process of loading it (the first time it's imported). Some modules are designed purely to be loaded for their side-effects (what their top-level code does). Your example is like that; its side effect is the console.log it does.

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