Chapter 8. Modular Programming

Modular programming is one of the most important and frequently used software design techniques. Unfortunately, JavaScript didn't support modules natively that lead JavaScript programmers to use alternative techniques to achieve modular programming in JavaScript. But now, ES6 brings modules in to JavaScript officially.

This chapter is all about how to create and import JavaScript modules. In this chapter, we will first learn how the modules were created earlier, and then we will jump to the new built-in module system that was introduced in ES6, known as the ES6 modules.

In this chapter, we'll cover:

  • What is modular programming?
  • The benefits of modular programming
  • The basics of IIFE modules, AMD, UMD, and CommonJS
  • Creating and importing the ES6 modules
  • The basics of the Modular Loader
  • Creating a basic JavaScript library using modules

The JavaScript modules in a nutshell

The practice of breaking down programs and libraries into modules is called modular programming.

In JavaScript, a module is a collection of related objects, functions, and other components of a program or library that are wrapped together and isolated from the scope of the rest of the program or library.

A module exports some variables to the outside program to let it access the components wrapped by the module. To use a module, a program needs to import the module and the variables exported by the module.

A module can also be split into further modules called as its submodules, thus creating a module hierarchy.

Modular programming has many benefits. Some benefits are:

  • It keeps our code both cleanly separated and organized by splitting into multiple modules
  • Modular programming leads to fewer global variables, that is, it eliminates the problem of global variables, because modules don't interface via the global scope, and each module has its own scope
  • Makes code reusability easier as importing and using the same modules in different projects is easier
  • It allows many programmers to collaborate on the same program or library, by making each programmer to work on a particular module with a particular functionality
  • Bugs in an application can easily be easily identified as they are localized to a particular module
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.59.36