Differences between exports and module. exports in nodejs

Source: Internet
Author: User
Tags dio

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)
Copy codeThe Code is as follows:
Exports. name = function (){
Console. log ('My name is lew.kilmister ');
};

In another file, you reference
Copy codeThe 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:
Copy codeThe Code is as follows:
Module. exports = 'Rock IT! ';
Exports. name = function (){
Console. log ('My name is lew.kilmister ');
};

Reference and execute rocker. js again
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
Module. exports = ['lew.kilmister', 'oss osbourne', 'Ronnie James Dio ', 'steven count', 'Mick jarger'];

You can apply it as follows:
Copy codeThe 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:
Copy codeThe Code is as follows:
Module. exports. name = function (){
Console. log ('My name is lew.kilmister ');
};

Similarly, exports is like this.
Copy codeThe 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.

Related Article

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.