Home  >  Article  >  Web Front-end  >  exports 和 module.exports

exports 和 module.exports

伊谢尔伦
伊谢尔伦Original
2016-11-21 13:20:56997browse

Although I haven’t used the modular writing method of commonJS in my work for the time being, I still often see the source code or articles of this writing method. Sometimes I feel a little confused when I see exports and module.exports. . . I think beginners may have the same questions as me, and after checking, there are really a lot of them. . I read a few articles, and a buddy on stackoverflow wrote a good one, so I recorded it here.

In fact, it is easy to distinguish by remembering and understanding this picture:


This picture shows the relationship between the two. In order to facilitate understanding, I think it can be explained like this. When a js file needs to be modularized At that time, the node environment will inject the two variables exports and module.exports into the file, and at the beginning these two variables point to the same empty object. Then after you perform a series of operations with these two variables , note that this is key: only module.exports will be returned for subsequent use by other modules require references.

So here we just explain the relationship between operation, so why do we need two? Yes, why? As long as module.exports is not there, it will be normal if you assign a value to it and return it. You have to make an exports What are you doing? . . . Haha, what I understand is, yes, you can always use module.exports and it will not go wrong, but do you feel tired every time you write:

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

?? Then write it like this:

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

Isn’t it more refreshing? Is it because I wrote less that I came up with exports? . . I think so, hahaha, programmers are extremely lazy. So here comes the problem, just use exports instead of module.exports, but be careful, otherwise you will make mistakes, for example:

exports = function A() {}


If you want to export a class, write it like this, think about the above In that picture, module.exports is returned. The assignment here breaks the relationship between exports and module.exports, so what do you get when you reference it? Yes, an empty object. . .

So what’s the conclusion?

When the stuff you want to export can be directly expanded on the empty object, using exports will certainly save time and effort

When the stuff you want to export needs to completely cover the empty object When you are confused, you can only use module.exports

Please use module.exports when you are confused. . .


Actually, you don’t have to be so troublesome. Every time you write, think about that picture and that sentence, and everything will go naturally. Hey, you have to study hard in order to write less code!!


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