Home  >  Article  >  Web Front-end  >  Detailed introduction to the difference between exports and module.exports in nodejs_Basic knowledge

Detailed introduction to the difference between exports and module.exports in nodejs_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:43:411499browse

You must be very familiar with the exports object in nodejs modules, you can use it to create your modules. For example: (assuming this is a rocker.js file)

Copy code The code is as follows:

exports .name = function() {
console.log('My name is Lemmy Kilmister');
};

In another file you quoted
Copy code The code is as follows:
var rocker = require('./rocker.js');
rocker.name(); // 'My name is Lemmy Kilmister'


So what exactly is Module.exports? Is it legal? In fact, Module.exports is the real interface, and exports is just an auxiliary tool for it. What is ultimately returned to the call is Module.exports instead of exports.

All attributes and methods collected by exports are assigned to Module.exports. Of course, there is a premise for this, that is, Module.exports itself does not have any properties and methods. If Module.exports already has some properties and methods, the information collected by exports will be ignored.

Modify rocker.js as follows:

Copy the code The code is as follows:
module.exports = 'ROCK IT!';
exports.name = function() {
console.log('My name is Lemmy Kilmister');
};

Reference and execute rocker.js again

Copy code The code is as follows:
var rocker = require( './rocker.js');
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'

Found error: Object "ROCK IT!" There is no name method
The rocker module ignores the name method collected by exports and returns a string "ROCK IT!". It follows that your module does not necessarily have to return an "instantiated object". Your module can be any legal JavaScript object - boolean, number, date, JSON, string, function, array, etc.

Your module can be anything you set to it. If you do not explicitly set any properties and methods to Module.exports, then your module will export the properties set to Module.exports.

In the following example, your module is a class:

Copy the code 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 ' years old');
};
};

You can apply it like this:

Copy code The code is as follows:
var Rocker = require('./rocker.js');
var r = new Rocker('Ozzy', 62);
r.about(); // Ozzy is 62 years old

In the following example, your module is an array:

Copy code The code is as follows:
module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];

You can apply it like this:

Copy code The code is as follows:
var rocker = require('./rocker.js');
console.log('Rockin in heaven: ' rocker[2]); //Rockin in heaven: Ronnie James Dio

Now you understand, if you want your module to be of a specific type use Module.exports. Use exports if the module you want is a classic "instantiated object".

Adding attributes to Module.exports is similar to adding attributes to exports. For example:

Copy code The code is as follows:
module.exports.name = function() {
console.log('My name is Lemmy Kilmister');
};


Similarly, exports are like this
Copy code The code is as follows:

exports.name = function() {
console.log('My name is Lemmy Kilmister');
};

Please note that these two results are not the same. As mentioned earlier, module.exports is the real interface, and exports is just its auxiliary tool. It is recommended to use exports unless you plan to change from the original "instantiated object" to a type.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn