The difference between module.export and export in node. js.

Source: Internet
Author: User

Some understanding of module.exports and exports

Probably the simplest and easiest-to-understand article about the module.exports and exports differences.

Exports = Module.exports = {};

所以module.exportsAnd exports The difference is var a={}; var b=a; that the difference between A and B

It seems that there is a lot of difference between wood, but the actual use of time but there is a difference, this is why, please listen to my carefully said

About module.exports and exports what is the difference, a search on the internet a lot, but said is too complex ...
I heard that exports is a reference to the Module.exports object (reference) ^1, what is a reference?! ... _ (: З"∠) _

Of course, if you want to thoroughly understand the differences between the two exports, it is best to look at the source code, see how it is encapsulated, the skill of the children's shoes should be a look at the understanding. However, the source code I also can not understand ... (?_?)

But recently feel assorted read a lot of articles to do a lot of experiments, like the opening of the two meridians, wit I seem a bit on the road ...

Module

The first thing to make clear is that the module is an object {Object} .
When you create a new file, such as Mo.js, the file reads as follows:

1
Console.log (module);

Then execute the file in cmd node mo.js , you can see that the module is actually a module instance, you can understand, Nodejs defines a module class, there are many properties and methods, exports is one of the properties:

1
2
3
4
5
{
ID:' BlaBla ',
Exports: {},
BlaBla ...
}

When each JS file is executed or is require, Nodejs actually creates a new instance var module = new Module() named module .
That's why you don't define module this variable, but you can console.log it out without causing an error .

Module.exports

Suppose I have a JS file with the following contents:

console.log(module); //你会看到Module中的exports为空对象{}module.exports = {  print : function(){console.log(12345)}}console.log(module); //你会看到Module中的exports对象已经有了print()方法

With the above foundation, it is easy to understand that you module.export are actually adding methods/properties to the exports object in the module instance .

Exports

This is usually used when using exports:

function(){console.log(12345)}

Suppose I have a JS file with the following contents:

 console.log (module); //you will see the exports in module is empty object {}console.log (exports); //you will see the exports in module is empty object {}module.exports = {print: function (console.log ( 12345)}} console.log (module); //you will see the exports object in module with the print () method exports.name = console.log (module); //you will see that the exports object in module has not only the print () method, but also the Name property        

It can also be seen that the legend of the exports fact is module.exports the reference , you can understand, Nodejs in your code before quietly added the following code:

var module = new Module();var exports = module.exports;

That's why you don't define exports this variable, but you can console.log it out without causing an error .

Require

When you call a module from outside, what is require actually require? ^2
Require when Nodejs will go everywhere to find whether this module, if there is, return is module.exports in the east.

DOs & Donts
  • √ You can do this:
    module.exports.name = ‘小白妹妹‘;exports.age = 10;module.exports.print = function(){console.log(12345)};
    If you just use . to add properties and methods, module.exports and exports mixing is completely possible, in this case, the feeling exports is for lazy people to use ... After all, we can write a few 7 characters less!
  • √ You can also do this:
    module.exports = {name = ‘小白妹妹‘;};exports.age = 10;module.exports.print = function(){console.log(12345)};
  • X But this is not possible :
    module.exports = {name = ‘小白妹妹‘;};exports = {age:10}; // exports现在是{age:10}这个对象的引用,不再是module.exports的引用了console.log(module); //你会看到Module的exports中只有name属性!!!
  • x is also not possible :
    exports.age = 10; console.log(module); //你会看到Module的exports中多了age属性module.exports = {name = ‘小白妹妹‘;};console.log(module); //你会看到Module的exports中还是只有name属性!!!
    Summary or that sentence, module.exports and exports The difference is var a={}; var b=a; that the difference between A and B
    • The changes are exports not valid after the point exports.xxx is added. Because the only require returned would bemodule.exports
  • You cannot exports.xxx change the point after it has been used module.exports . Because the exports.xxx added properties and methods do not exist module.exports in the new object that you are pointing to.
  • For properties to export, you can simply hang directly onto the exports object
  • For a class, to directly make the exported content the constructor of the class allows the caller to create an instance object using the new operator, the constructor should be hung module.exports on the object and not mixed with the exported property values

I feel that I said it is very clear ~
Whether you are clear or not, I am clear anyway. _ (: З"∠) _

The difference between module.export and export in node. js.

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.