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

167
Visualizações
Are ES Modules evaluated to their very end before export?

I want be able to export and then modify a object. However I am not sure if this is unexpected behavior.

test.js:

// Short syntax
export const foo = [];
foo[0] = 1;

// Long syntax
const bar = [];
bar[0] = 1;
export { bar };

index.js:

import { foo, bar } from "./test.js";

console.log("foo:" + foo); // Is it guaranteed to be [1]? Or can it be []?
console.log("bar:" + bar); // I always expect this to be [1].

This might be a foolish example but sometimes I have an object and want to register some methods/properties. I want to know if I am able to export it and then register them.

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

0

Are ES modules evaluated to their very end before export?

No. In fact, the exports are declared and registered before the module is evaluated.

However, what you actually seem to be asking is

Are ES modules evaluated to their very end before the modules importing them are evaluated?

Yes. test.js will be fully evaluated before index.js is evaluated1. It doesn't matter where in the module you place the import and export declarations.

1: with the exception of circular dependencies. In that case, a module might run before all the imported modules are evaluated, and you may run into uninitialised variables.

Is it guaranteed to be [1]? Or can it be []?

Yes, it's guaranteed to be [1] (or an exception that const foo is not yet initialised). It will never be [], the = [] and the foo[0] = 1 always execute right after each other.

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