getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.87k stars 33.44k forks source link

Editorial Review, Scope & Closures, Chapter 8 #1612

Closed simonstl closed 4 years ago

simonstl commented 4 years ago

Editorial review, not a searchable issue.

Edition: (1st or 2nd) 2nd

Book Title: Scope & Closures

Chapter: 8

General

This is a fine natural conclusion to the book. I wondered if it might even better be titled The Module and Namespaces Patterns, since Namespaces are the currently secret bonus. However, I think it's probably fine to leave it with just Modules, as Modules are the more common approach. The only thing I see missing is a brief explanation of why to choose one module format over another. This keeps changing, of course!

Opening

While I like the valedictory address, too many readers are likely to be reading this chapter in particular in isolation. Could you reorganize this intro by focusing on modules in the opening sentence, and then talking about how the rest of the subjects in the book have made this story possible?

I think you'll have better luck getting people who arrive specifically for modules to read both this chapter and the rest that way, without losing readers who've been along for the entire book.

Encapsulation and POLE

The explanation is great, but my paranoia over readers coming to this chapter separately to figure out modules makes me want to see the POLE acronym expanded again. Could you work principle of least exposure into the text?

What is a Module?

I got all excited about namespaces, because I generally prefer to isolate state from logic whenever possible. (I also find that approach reduces the amount of necessary encapsulation, though it doesn't go away.) I think namespaces are worth calling out in a separate section that comes before modules, since they're simpler. Maybe title it: Namespaces: Grouping Logic, and then have Modules: Enscapsulating Logic and State ?

My excitement aside, I also think that reduces the amount of "what ISN'T a module" on the front of the core modules section, and lets people get to the core issues faster if they're skimming.

Classic Modules

This is the explanation I wish I had a decade ago. You do a great job of integrating IIFE, singletons, closure, and the question of how to present an API from inside the module. The example is readable - I think, even for people who haven't read the rest - and a solid foundation for the rest of the conversation. The Module Factory discussion is also helpful. I thought it might make sense to split into its own section, but that's probably too much.

Node CommonJS Modules

This bridges the code structure approach of the classic modules with the file-based approach nicely, and explains the singleton/IIFE/closure story well in the new context.

Modern ES Modules (ESM)

Again, this builds smoothly. The prior section explained most of the internal details, and this can focus on the external connectivity.

Exit Scope

This feels like we've reached the top of the mountain. Congratulations! (Yes, appendices yet to come.)