Skip to Content
JavaScript: The Definitive Guide, 7th Edition
book

JavaScript: The Definitive Guide, 7th Edition

by David Flanagan
May 2020
Intermediate to advanced
706 pages
21h 15m
English
O'Reilly Media, Inc.
Book available
Content preview from JavaScript: The Definitive Guide, 7th Edition

Chapter 10. Modules

The goal of modular programming is to allow large programs to be assembled using modules of code from disparate authors and sources and for all of that code to run correctly even in the presence of code that the various module authors did not anticipate. As a practical matter, modularity is mostly about encapsulating or hiding private implementation details and keeping the global namespace tidy so that modules cannot accidentally modify the variables, functions, and classes defined by other modules.

Until recently, JavaScript had no built-in support for modules, and programmers working on large code bases did their best to use the weak modularity available through classes, objects, and closures. Closure-based modularity, with support from code-bundling tools, led to a practical form of modularity based on a require() function, which was adopted by Node. require()-based modules are a fundamental part of the Node programming environment but were never adopted as an official part of the JavaScript language. Instead, ES6 defines modules using import and export keywords. Although import and export have been part of the language for years, they were only implemented by web browsers and Node relatively recently. And, as a practical matter, JavaScript modularity still depends on code-bundling tools.

The sections that follow cover:

  • Do-it-yourself modules with classes, objects, and closures

  • Node modules using require()

  • ES6 modules using export, import, and import() ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern JavaScript from The Beginning [Second Edition] - Second Edition

Modern JavaScript from The Beginning [Second Edition] - Second Edition

Brad Traversy
JavaScript from Beginner to Professional

JavaScript from Beginner to Professional

Rob Percival, Laurence Svekis, Maaike van Putten, Codestars By Rob Percival

Publisher Resources

ISBN: 9781491952016Errata PageSupplemental Content