Imports serve as read-only connections to the original variable located in the exporting module. The term "read-only" signifies that direct modifications are not possible. On the other hand, the term "live" indicates that any alterations made by the exporting module can be observed.
In cases where a module needs to permit other modules to adjust the values of its exports (which is typically infrequent), it must export a function intended for that purpose. For example:
alpha.js
:
export let folder;
export function setFolder(f) {
folder = f;
}
beta.js
:
import { folder, setFolder } from "./alpha.js";
console.log(folder); // The output will be `undefined` unless altered by another module
setFolder(gui.addFolder("Avatar Measurements"));
console.log(folder); // The output will reflect whatever `gui.addFolder` returns
However, it remains highly unusual for a module to allow external modifications to its exports in this manner.
As requested, here is an instance of exporting an object instead:
alpha.js
:
export let shapes = {};
beta.js
:
import { shapes } from "./alpha.js";
shapes.folder = gui.addFolder("Avatar Measurements");
It is not immediately evident why this object must reside in alpha
rather than being local in beta
.