Exports and module. exports

Source: Internet
Author: User
From exports and module. although the commonJS modular writing method is not used in the current work, the source code or article of this writing method is often seen, and sometimes exports and module are seen. exports is a bit silly... I think beginners may have the same questions as me. It is really difficult to check the results .. I have read several articles, and stackoverflow has a buddy who writes well, so it is recorded here.

In fact, it is easy to distinguish between remembering and understanding this image:


This figure shows the relationship between the two. For ease of understanding, I think this can be explained. When a js file needs to be modularized, the node environment will inject exports and modules to the file. the two variables exports point to the same empty object at the beginning, and then you take these two variables for a series of operations, note that this is the key: Only module. exports will be returned for reference by other modules.

So here we just explained the Running Relationship. Why do we get two? Yes. Why? As long as module. exports is not enough, it is also much more normal if you assign a value to it to return, so you have to get an exports plug-in .... Haha, I understand that, yes, you can always use module. exports without errors, but every time you write this:

module.exports.a = 1;  module.exports.b = function() {};  module.exports.c = 'ccc';

Are you tired ?? Then write it like this:

exports.a = 1;  exports.b = function() {};  exports.c = 'ccc';

Is it refreshing? ? Is it because I have to write less to get the exports... I think it seems like Haha, programmers are very lazy. So the problem is that all exports are used, so you don't need module. exports. You should be careful. If this problem is not solved, errors may occur. For example:

exports = function A() {}


You want to export a class and write it like this. If you think about the above figure, the module is returned. exports. The value assignment here refers to exports and module. the exports relationship is broken, so what do you get when you reference it? That's right. It's an empty object...

So what is the conclusion?

When you want to export something that can be expanded directly on an empty object, use exports to save time and effort.

When you want to completely overwrite the empty object, you can only use module. exports.

Use module. exports when you cannot tell clearly...


In fact, you don't need to be so troublesome. Every time you write a text, you can think about the picture and the sentence. Everything goes with the flow. No, ah ~ In order to write less code, you should study it !!

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.