Differences between exports and module. exports in nodejs

Source: Internet
Author: User
Tags dio
I have read many articles about exports and module. the difference between exports is not accurate enough. The difference between the two is: 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 e... syntaxHighlighter. all read many articles about exports and module. the difference between exports is not accurate enough. The difference between the two is: 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. 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 rocker. js file) the code is as follows: exports. name = function () {console. log ('My name is lew.kilmister ');}; in another file, you reference the Code as follows: var rocker = require ('. /rocker. js'); rocker. name (); // 'My name is lew.kilmister. what is 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: Code: module. exports = 'Rock IT! '; Exports. name = function () {console. log ('My name is lew.kilmister ') ;}; re-reference and execute rocker. the js Code is as follows: var rocker = require ('. /rocker. js'); rocker. name (); // TypeError: Object rock it! Has no method 'name' error: 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: the 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 like this: the code is as follows: var Rocker = require ('. /rocker. js'); var r = new Rocker ('osscs', 62); r. about (); // oearis 62 years old in the following example, your module is an array: the code is as follows: module. exports = ['leechkilmister', 'oss osbourne', 'Ronnie James Dio ',' Steven Tyler ', 'Mick jarger']; you can apply it like this: the code is as follows: var rocker = require ('. /rocker. js'); console. log ('rockin heaven: '+ rocker [2]); // Rockin heaven: Ronnie James Dio now you understand, 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, the Code is as follows: module. exports. name = function () {console. log ('My name is lew.kilmister ') ;}; www.2cto.com Similarly, the exports 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.