1. Disadvantages of JavaScript development

There are two major problems with JavaScript: file dependencies and naming conflicts.

2. Modular development in life

3. Modularization development in software

A function is a module, multiple modules can form a complete application, the removal of a module will not affect the operation of other functions.

4. Modular development specification in Node.js

  • Node.js specifies that a JavaScript file is a module, and variables and functions defined inside the module are not available externally by default
  • Members can be exported from within a module using the Exports object and imported from other modules using the require method.

5. Export module members

// a.js
// Define variables inside the module
let version 1.0
// Define methods inside the module
constSayHi name ${// Export data outside the module
exports.version version
exports.sayHi sayHi
Copy the code

6. Import module members

// b.js
// Import module A into module B. js
let a require (('./b.js');
// Outputs the version variable in module B
console.log(a.version)
// Call the sayHi method in module B and print its return value
console.log(A.sayhi dark horse lecturer' '));
Copy the code

You can omit the suffix when importing modules

7. Another way to export module members

module.exports.version = version; 
module.exports.sayHi = sayHi;
Copy the code

Exports is an alias (address reference relationship) of module.exports

8. The module exports the connection and difference between the two methods

exports.version = version; 
module.exports.version = version;

module.exports = {
    name: 'zhangsan',}Copy the code