JavaScript modules 101

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 sub-modules, thus creating a module hierarchy.

Modular programming has many benefits. Some benefits are as follows:

  • It keeps our code both cleanly separated and organized by splitting it 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
  • It 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 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.16.125.26