The difference between module.exports and exports

Source: Internet
Author: User

This question nearly half a year to see three times, has been a bit of a daze, today look at park and some stack above the information finally is understood point, first record a bit lest forget.

Plain English: exports is just a reference to Module.exports, they all point to the same address in memory.

When node loads the module, each module defaults to a modules object that has a property exports; The initial value is an empty object {}, which can be thought of as the object that the module.exports eventually points to.

--so how to understand exports is just a module.exports reference. You can see that.

   var module.exports={},       exports=module.exports; //此时exports 和module.exports指向内存中同一块区域   //我们导出一个函数当使用exports的时候.像这样:   exports=function(){ console.log(‘export function‘); } //此时 exports指向的已经是内存中另一块区域了,它已经和module.exports={},赋值的对象没有任何关系,此时他们关系如此密切的两个好基友再无瓜葛。我们使用require()然后调用会报错。 //这种情况导出就可以工作很好理解,就是给最初的对象增加了一个属性sayHello而已. exports.sayHello=function(){ console.log(‘hello‘); }

In fact, it is similar to the reference type assignment and modification in Java.

In StackOverflow see the explanation of the Special 6 please see:

var module = { exports: {} };var exports = module.exports;// your codereturn module.exports;

For reference, please click:
Https://cnodejs.org/topic/5231a630101e574521e45ef8

Difference between module.exports and exports

The difference between module.exports and exports

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.