So I have this file called lets say "fruits.js"
In this module, I have a global variable "name" with value "apples" I also have a getter and setter for this variable to get the value of the variable name and to set a new to it.
now I imported this module into a file called "client1.js", and ran getter, it showed "apples".
now I ran setter with value "coconuts" and changed the variable.
now after client1.js finishes the execution, client2.js starts the execution with the same copied code.
and instead of "apples" it shows "coconuts".
So my question is, do multiple imports of the same module share the same lexical scope ?
code -
// fruits.js
let fruit = "apples";
const set = () => {
let flower = "lily";
const setfruit = (newfruit) => {
fruit = newfruit;
}
const getfruit = () => {
return fruit;
}
const setflower = (newflower) => {
flower = newflower;
}
const getflower = () => {
return flower;
}
return {
setfruit,
getfruit,
setflower,
getflower
}
}
module.exports = set();
// client1.js
const fruitsnflowers = require("./closure.js")
const execute = () => {
console.log(fruitsnflowers.getfruit())
console.log(fruitsnflowers.getflower())
fruitsnflowers.setfruit("coconut")
fruitsnflowers.setflower("hibiscus")
}
module.exports = execute;
// client2.js
const fruitsnflowers = require("./closure.js")
const execute = () => {
console.log(fruitsnflowers.getfruit())
console.log(fruitsnflowers.getflower())
fruitsnflowers.setfruit("papaya")
fruitsnflowers.setflower("lotus")
}
module.exports = execute;
// driver.js
const client1 = require("./client1.js")
const client2 = require("./client2.js")
client1()
client2()
// output
~ node driver.js
apples
lily
coconut
hibiscus
CodePudding user response:
The module's code is loaded and initialized only once when it is first imported. After that, the exact same module handle is returned from subsequent attempts to import it.
So, in your case, when you do module.exports = set()
, you are running that functions set()
which returns an object that has references to local functions that can see the local scope. Anyone who imports the function will get the exact same exported object so therefore, they will all point to the same local scope (within this module).
If you call setFruit("banana")
from within moduleA and then call getFruit()
from within moduleB, you will get back "banana"
. The fruits module only has one set of state and only one internal fruits
variable that all calls to it's exported methods have access to.
So my question is, do multiple imports of the same module share the same lexical scope ?
Your use of the term "lexical scope" here is a bit confusing. Two modules that import this other module each have their own lexical scope, but the code inside the imported module lives in its own scope that does not change no matter who imports it.
So, if moduleA and moduleB each import your fruits module, there will be three scopes as each module has its own lexical scope at the top level. When you call a fruit function, that function operates internally (inside the execution of the function) within the fruit lexical scope. When you get the return value back from within moduleA or moduleB, you are then operating within those module's scope. There is no mingling of the scopes. Each module has its own top level scope.