If I defined a function that depends on (uses) a global variable in its module, what would happen if I exported it and tried to call it outside that module?
I have tried it and found that it just works normally, although I don't have access to that variable (it's not defined in my scope). So I want to make sure whether I understand this or not, is this just like the closure functions and the function gets its variables from the lexical scope?
PS: My question is related to the ES6 modules feature.
#Adding real simple Example
This is my html: simply nothing but a script
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script type="module" src="app.js"></script>
</body>
</html>
This is my module file:
let globalVariable = "I cannot be accessed outised this module";
export function testFunction() {
console.log(globalVariable);
}
This my app.js file:
import { testFunction } from "./module.js";
testFunction();
Although globalVariable is not accessible in app.js the message has been printed in the console.