Learning the module specification of node. js from zero (I)

  • 2021-07-22 08:46:31
  • OfStack

What is Node. js?

Many beginners don't really understand what Node. js is. The descriptions in the nodejs. org website are not very helpful either.

First of all, it should be clear that Node is not an Web server, and these 10 points are important. It can't do anything by itself. It doesn't work like Apache. If you want it to be an HTTP server, you must write it yourself with the help of its built-in library. Node. js is just another way to execute code on a computer. It is a simple JavaScript Runtime.

Modularization

Before explaining the concepts of CommonJS, AMD and CMD, let's first understand the modularity of js. Modularization, as its name implies, is to decompose the project according to functions or other logic, each part only deals with one function, and decouples the functions to facilitate future development and maintenance. Then modularization must have the following capabilities in order to split and assemble modules:

Define encapsulated modules;
Define the dependencies of new modules on other modules;
Support for the introduction of other modules;

Then a set of specifications is needed to define these capabilities, so CommonJS, AMD, CMD and so on appear.

1. CommonJS

CommonJS was originally called ServerJS, which is the specification of js on the server side, and node uses this specification. According to the CommonJS specification, a single file is a module, require is used to load a module, and exports is used to expose the methods or attributes in the module to the outside world.

For example:


// hello.js
function say(username){
 console.log( 'hello, '+username );
}

exports.say = say; 

=============


// main.js
var person = require('./hello');

person.say('wenzi'); // hello, wenzi
person.say(' Shi Shaobing '); // hello,  Shi Shaobing 
person.say('NUC'); // hello, NUC 

At the same time, require statements can be written anywhere in the file, as long as before the use of the previous reference can, no 1 must be written in the front of the file. However, in order to make the code easier to read and intuitively see which modules are currently referenced, it is best to put them at the front of the file.

Differences between EXPORTS and MODULE. EXPORTS

Someone may have seen direct use exports Yes, some are used module.exports Yes, here's a little explanation of the difference between the two.

Let's give a simple example:


var a = {name:'wenzi'};
var b = a;

console.log(a); // {name: "wenzi"}
console.log(b); // {name: "wenzi"} 

The output of a and b is sample 1. Now I change the value of name in b:


b.name = 'shaobing';

console.log(a); // {name: "shaobing"}
console.log(b); // {name: "shaobing"} 

The output of a and b has changed. I would like to restate b:


var b = {name:' Shi Shaobing '};

console.log(a); // {name: "shaobing"}
console.log(b); // {name: " Shi Shaobing "} 

These three examples output three results:

Declare an a object, assign a to b, and then a and b output the same result; If name in b is changed, name in a will also change; If the b object is re-declared, name in a does not change with b1

Explanation: a is an object, and b is a reference to a, that is, a and b point to the same block of memory, so the output in 1 is a sample. When b is modified, that is, the contents of a and b pointing to the same memory address are changed, and a will also be reflected, so the output of the second example is also the same. When b is overwritten, b points to a new block of memory, and a still points to the original memory, so the final output will be different.

Then at this time, you can lead to exports And module.exports Got it:

module.exports Initial value is 1 empty object {} exports Is pointing module.exports Reference to require() Returns the module.exports Instead of exports

If module.exports A new direction occurs, then exports Invalid; If module.exports If there is no change, it will be directly exports That's enough.

2. AMD and RequireJS

When it comes to AMD, we have to say that RequireJS and AMD are independent from CommonJS community and become AMD community alone. The popularity of AMD also relies on the promotion of RequireJS authors to a great extent.

In the AMD specification, the default recommended module format is:


// hello.js
//  Write all the modules that need to be introduced into the array, and then pass parameters to call 
define(['a', 'b'], function(a, ,b){
 // do something

 return{
 hello : function(username){
  console.log( 'hello, '+username );
 }
 }
})

==========


// main.js
define(['./hello'], function(h){
 h.hello('wenzi');
})

That is to say, in AMD, modules must be defined using define, and dependencies are passed in through function parameters. One advantage of this is that all dependencies can be seen at first sight.

3. CMD and seajs

The CMD specification is proposed by the famous Yu Bo God in China. What you will do is to rely on it nearby. When you use it, you will carry out it in that place require . SeaJS is the CMD specification used:


// hello.js
define(function(require, exports, module){
 var a = require('a');
 // do a

 var b = require( 'b' );
 // do b

 module.exports.hello = hello; //  External export hello
})

You can also see the difference between AMD and CMD from here:

AMD usually needs to introduce all dependencies once and then pass them through parameters; While CMD is introduced when necessary

However, AMD also supports an incoming format such as CMD, but it is executed internally according to the logic of AMD.

4. Summary

This article introduces the differences and connections between CommonJS, AMD and CMD specifications, and briefly summarizes them here:

CommonJS: Each file is a module, not defined by define, node uses this specification; AMD: Use define to define a module, paying attention to dependence in advance; CMD: Use define to define a module that will depend on the nearest

Ok, that's all of this article. Next, we will start to learn node formally. Interested friends, please continue to pay attention to this site.


Related articles: