You must be very familiar with the exports object in the nodejs module. You can use it to create your module. Next, we will introduce the creation process. If you are interested, you can refer to it. You must be very familiar with the exports object in the nodejs module, you can use it to create your module. Example: (assume this is the rocker. js file)
The Code is as follows:
Exports. name = function (){
Console. log ('My name is lew.kilmister ');
};
In another file, you reference
The Code is as follows:
Var rocker = require ('./rocker. js ');
Rocker. name (); // 'My name is leader Kilmister'
So what is Module. exports? Is it legal?
In fact, Module. exports is the real interface, and exports is only a helper tool. The Module. exports instead of exports is returned.
All the properties and Methods Collected by exports are assigned to Module. exports. Of course, there is a premise that Module. exports itself does not have any attributes and methods. If Module. exports already has some attributes and methods, the information collected by exports will be ignored.
Modify rocker. js as follows:
The Code is as follows:
Module. exports = 'Rock IT! ';
Exports. name = function (){
Console. log ('My name is lew.kilmister ');
};
Reference and execute rocker. js again
The Code is as follows:
Var rocker = require ('./rocker. js ');
Rocker. name (); // TypeError: Object rock it! Has no method 'name'
Error reported: object "rock it !" No name Method
The rocker module ignores the name method collected by exports and returns a string "rock it !". We can see that your module does not have to return an "instantiated object ". Your module can be any legal javascript Object-boolean, number, date, JSON, string, function, array, and so on.
Your module can be anything you set for it. If you have not explicitly set any attributes and methods for Module. exports, your Module is the attribute set for Module. exports.
In the following example, your module is a class:
The Code is as follows:
Module. exports = function (name, age ){
This. name = name;
This. age = age;
This. about = function (){
Console. log (this. name + 'is + this. age + 'ears old ');
};
};
You can apply it as follows:
The Code is as follows:
Var Rocker = require ('./rocker. js ');
Var r = new Rocker ('oss ', 62 );
R. about (); // oearis 62 years old
In the following example, your module is an array:
The Code is as follows:
Module. exports = ['lew.kilmister', 'oss osbourne', 'Ronnie James Dio ', 'steven count', 'Mick jarger'];
You can apply it as follows:
The Code is as follows:
Var rocker = require ('./rocker. js ');
Console. log ('rockin heaven: '+ rocker [2]); // Rockin heaven: Ronnie James Dio
Now you understand that if you want your Module to be of a specific type, use Module. exports. If the module you want is a typical "instantiated object", use exports.
Adding properties to Module. exports is similar to adding properties to exports. For example:
The Code is as follows:
Module. exports. name = function (){
Console. log ('My name is lew.kilmister ');
};
Similarly, exports is like this.
The Code is as follows:
Exports. name = function (){
Console. log ('My name is lew.kilmister ');
};
Note that these two results are not the same. As mentioned above, module. exports is a real interface, and exports is only a helper tool. We recommend that you use exports for export, unless you want to change from the original "instantiated object" to a type.