There is no detailed explanation of what exactly imports and exports es6 under the hood. Someone describes import as read-only viewing. Check out the code below:
// lib/counter.js export let counter = 1; export function increment() { counter++; } export function decrement() { counter--; } // src/main.js import * as counter from '../../counter'; console.log(counter.counter); // 1 counter.increment(); console..log(counter.counter); // 2
My question is: if two modules import the same counter module and the first module increments the counter, will the second module also see the counter as incremented? What to do with the hood "import" and "export"? In what context is the increment function performed? What is an increment function variable object?
// lib/counter.js export let counter = 1; export function increment() { counter++; } export function decrement() { counter--; } // src/main1.js import * as counter from '../../counter'; console.log(counter.counter); // 1 counter.increment(); console..log(counter.counter); // 2 // src/main2.js import * as counter from '../../counter'; console.log(counter.counter); // what is the result of this, 1 or 2?
It seems to me that the βexportβ creates a global object that various modules can access, and it sets the context of the exported function for this object. If so, the design is related to me because the modules do not know what other modules do. If two modules import the same module (counter), one module calls the increment function (example above), which cause a change in value (counter), the other module does not know this.
source share