The difference of exports and Module.exports in Nodejs detailed Introduction _ Basics

Source: Internet
Author: User
Tags dio
You must be very familiar with the exports object in the Nodejs module, and you can use it to create your module. For example: (assuming this is a rocker.js file)
Copy Code code as follows:

Exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

In another file you quote like this
Copy Code code 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 one of its auxiliary tools. The final return to the call is module.exports rather than exports.

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

Modify Rocker.js as follows:
Copy Code code as follows:

Module.exports = ' Rock it! ';
Exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

Referencing the execution rocker.js again
Copy Code code as follows:

var rocker = require ('./rocker.js ');
Rocker.name (); Typeerror:object Rock It! Has no method ' name '

Error found: Object "Rock it!" No Name method
The rocker module ignores the exports collection's name method and returns a string "Rock it!". As it is, your module does not necessarily have to return "instantiated objects". Your module can be any legitimate JavaScript object--boolean, number, date, JSON, string, function, array, and so on.

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

In the following example, your module is a class:
Copy Code code 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 this way:
Copy Code code as follows:

var rocker = require ('./rocker.js ');
var r = new Rocker (' Ozzy ', 62);
R.about (); Ozzy is years old

In the following example, your module is an array:
Copy Code code as follows:

Module.exports = [' Lemmy Kilmister ', ' Ozzy Osbourne ', ' Ronnie James Dio ', ' Steven Tyler ', ' Mick Jagger '];

You can apply it this way:
Copy Code code as follows:

var rocker = require ('./rocker.js ');
Console.log (' Rockin in Heaven: ' + rocker[2]); Rockin in Heaven:ronnie James Dio

Now you understand that if you want your module to be a specific type, use Module.exports. If the module you want is a typical "instantiated object", use exports.

Adding attributes to Module.exports is similar to adding attributes to exports. For example:
Copy Code code as follows:

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

Again, exports is like this.
Copy Code code as follows:

Exports.name = function () {
Console.log (' My name is Lemmy Kilmister ');
};

Please note that these two results do not want to be the same. As mentioned earlier, Module.exports is a real interface, and exports is nothing more than its auxiliary tool. It is recommended to use exports export unless you intend to change from the original "instantiated object" to a type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.