JavaScript modularity

Article directory

  • 1. Understanding modularity
    • 1.What is modularity
    • 2. The history of modularity
    • 3. No problems caused by modularity
  • 2. CommonJS and Node
    • 1. CommonJS specification and Node relationship
    • 2. Modular case
    • 3.exports export
    • 4.module.exports export
    • 5. Change the imported object
    • 6.require search module rules
    • 7. Module loading process
  • 3. AMD
    • 1.Disadvantages of CommonJS specification
    • 2.AMD specifications
    • 3.Usage of require.js
  • 4. CMD specifications
    • 1.CMD specification
    • 2.Usage of SeaJS
  • 5. ES Module
    • 1. Get to know ES Module
    • 2. Case code problem analysis
    • 3.export keyword
    • 4.import keyword
    • 5. Use export and import in combination
    • 6.default usage
    • 7.import function
  • 6. ES Module implementation principle
    • 1.ES Module analysis process
    • 2. Phase 1: Construction Phase
    • 3. Phases 2 and 3: Instantiation Phase – Evaluation Phase

1. Understand modularity

1. What is modularity

What exactly are modularization and modular development?

  • In fact, the ultimate goal of modular development is to divide the program into small structures;

  • Write your own logical code in this structure and have its own scope. When defining variable nouns, will not affect other structures;

  • This structure can export the variables, functions, objects, etc. that you want to expose to its structure for use;

  • You can also import variables, functions, objects, etc. in other structures in a certain way;

The structure mentioned above is a module; the process of developing a program according to this structure is the process of modular development;

No matter how much you love JavaScript and how well it’s developed, it has a lot of flaws:

  • For example, the problem of variable scope defined by var;
  • For example, the object-oriented nature of JavaScript cannot use classes like conventional object-oriented languages;
  • For example, JavaScript does not have modularity issues;

For the early JavaScript without modularity, it did bring a lot of problems;

2. The history of modularization

In the early days of web development, Brendan Eich developed JavaScript only as a scripting language to do some simple form validation or animation implementation, etc., that The code is still very small:

  • At this time, we only need to write the JavaScript code into the