The Secret Life of JavaScript: Modules
dev.to·3h·
Discuss: DEV
📚Self-Documenting Code
Preview
Report Post

Why import creates a "Live Binding" and how ESM differs from CommonJS.


Timothy was standing at the chalkboard, looking frustrated. He had written three different files for his application, but they were crashing.

// user.js
var name = "Timothy";

// admin.js
var name = "Admin"; // Error! or Overwrite!

// main.js
console.log(name); // Which one is it?


"I keep overwriting my own variables," Timothy said. "Every time I add a script tag, I pollute the global namespace."

Margaret walked in. She didn’t hold a prop. She just pointed to the code.

"That is because you are not using Modules," she said. "You are loading scripts into the Global Scope. We need to encapsulate your code."

The Old World vs. The Module

Margaret wiped the board clean...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help