Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report

0

Short answer: Yes, that's possible.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!