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

146
Visualizações
Does imported functions from the same source, have access to their source global variables?

OK i searched every site i knew, using every keyword i knew, but i found nothing close the subject i searched about, maybe the whole question is wrong, but anyway...

let's say we have a module named Index.js, which contains a STORAGE and two functions responsible for reading/writing to STORAGE.

var STORAGE = []

function writeStorage(data) {
  STORAGE.push(data)
}

function readStorage(data) {
  //find data in STORAGE... and return result as RES
  return RES
}

Now, inside a file named write.js, we import writeStorage().

While in another file named read.js, we import readStorage().

And this is where the question rises,

Does imported functions from the same source, have access to their source global variables ?

i.e, can we modify STORAGE in index.js, using writeStorage() which we imported in write.js ?

and read STORAGE changes from read.js using readStorage() ?.

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

0

I want to provide clearer answer about that. There are two scopes for functions: lexical, and dynamic. Dynamic means, a function will search variables in the area, where it was called:

let someVar = 'global';

function dynamicScope () {
    someVar = 'dynamic';
    console.log(somevar);
}

dynamicalScope(); // will display 'global'

Lexical means, the function will search variables where it was written/declared:

let someVar = 'global';

function lexicalScope () {
    someVar = 'lexical';
    console.log(somevar);
} 

lexicalScope(); // will display 'lexical'

But in JavaScript, there is no way to use dynamic scope, so all function will search variables where it has been declared/written.

DO NOT PAY ATTENTION ON DETAILS, IT'S JUST A PSEUDO-CODE.

about 4 years ago · Juan Pablo Isaza Relatório

0

Short answer: Yes, that's possible.

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