I am migrating code that used to not use native JS modules.
// File: form.js
// `lc` is a global object I attach each file's functions to.
let lc = window.lc || {}
lc.form = {};
lc.form.add = function() {
// add a form
}
And then would be called as lc.form.add(), which is nice because the word add has context.
However now that I am changing the code to use JS modules:
// File: form.mjs
export function add() {
// add a form
}
And then would be called as add(), which has no context. Now I understand I could do:
import {add as formAdd} from '/form.mjs'
But thats like a step backwards to start smurf naming all imported things.
Is there a way to import the module like in Python, and then call functions of the modules. E.g. use dot etc notation to access it's function, something like this:
import '/form.mjs' as form
form.add()
You're looking for a namespace import:
import * as form from '/form.mjs';
form.add();