This is the 12th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

Modularity in Node

What is modularity

  • Js was designed for simple functions such as form validation, not modular solutions, so JS does not support modularity by nature
  • No modularity support simply means that you cannot import another JS file inside a JS file
  • In order to make JS support modularity, some great minds write their own low-level library files, let JS start to support modularity
  • There are four standards for the JS modularity specification: AMD CMD CommonJS ES6
  • Node belongs to the CommonJS standard
  • Using modularity can be a good solution to the variable, function name conflict, but also flexible to solve the problem of file dependency

Global scope and local scope

Local scope (the scope of a module) :

  • A JS file is a module
  • By default, properties (variables, constants) and methods defined in a JS file can only be used in the current JS file

Example: Create two modules: index.js and user.js. Attributes and methods are defined in the user.js module, and index.js imports the user.js module

  1. Create the users.js file
// Variables, constants, methods, etc. defined in a js file can only be used in that file
// This is called local scoping
// A js file is a module
const name = 'zs';
const person = {
    name: 'Wang Chongyang'.age: 50.skill: 'The Nine Yin Scriptures'
}

function show () {
    console.log('My name is Wang Sledgehammer. I never thought I would join The Sound of The Scene.');
}
Copy the code
  1. Create the index.js file and load users.js
// Local scope test:
// Load users.js -- The path format must be used when loading the custom module
const user = require('./users.js');

console.log(user);  // The result is {}

show();   // The result is undefined show

console.log(person.skill);
Copy the code

Execution result:

Global scope:

  • If all properties and methods declared in js files are mounted to the global object; When other JS files import the module, they can use the properties and methods in the module.

Example: Create two modules: index.js and game.js. The game.js module defines properties and methods and mounts them to the global object. Index.js imports the game.js module

  1. Create the game.js file and mount the data under the global object
// Mount variables, objects, and methods directly to the global object,
// This becomes the global scope
global.name = 'King of Glory';

global.obj = {
    name: 'lol'.company: 'tencent'.type: 'moba'
}

global.sayHi = function () {
    console.log('LOL is fun... ');
}
Copy the code
  1. Load the game.js file in the index.js file
// Global scope test:
require('./game.js');
console.log(global.name);

console.log(global.obj.name);

global.sayHi();
Copy the code

Execution result:

Module. exports export properties and methods

  • Mounting variables, objects, functions, and so on to global objects is not recommended because of variable contamination.
  • It is recommended to use module.exports to export variables, objects, and methods defined in modules
  • After loading (importing) a module using require, you can use the variables, objects, and methods defined in the module

Example: define variables, objects, methods in users.js, and export with module.exports

const name = 'zs';
const person = {
    name: 'Wang Chongyang'.age: 50.skill: 'The Nine Yin Scriptures'
}

function show () {
    console.log('My name is Wang Sledgehammer. I never thought I would join The Sound of The Scene.');
}

// Exports defined variables, objects, and methods using module.exports
module.exports = {
    name,
    person,
    show
}
Copy the code

Load the users.js module in index.js

// Use the path to load the third-party module

// Exports variables, objects, and methods as whole objects via module.exports

// It is stored in the user variable

const user = require('./users.js');

console.log(user.name);

console.log(user.person.name);

user.show();
Copy the code

Execution result:

Node and database links operate on the database below

Praise support, hand stay fragrance, and have glory yan, move your small hand to make a fortune yo, thank you for leaving your footprints.

Past excellent recommendation

Front ten thousand literal classics – basic

Front swastika area – advanced chapter

Let’s talk about two of the most common administrative tools used in front-end development

Talk about regular expressions

Hand touch hand take your liver Nodejs (1)

Obtain the file BLOB stream address to download the function

Hand touch hand take your liver NodeJS (2)

Git implements automatic push

Hand touch hand take your liver Nodejs (3)

Hand touch hand take your liver Nodejs (4)

Hand touch hand take your liver Nodejs (5)