[Node. js] Exporting Modules in Node

Source: Internet
Author: User

In this lesson, you'll learn the difference between the exports statement and Module.exports. Examples is demonstrated, each accomplishing the same task but one using export statements and one using Module.expor Ts. You'll also learn the basic thumb rule to identify which are appropriate for your current needs.

// Circle.js using the EXPORTS statement var Pi =function(r) {  return PI * R *function(r) {   return 2 * Pi * r;}

// accessing the exported functions in the node shell var circle = require ('./circle.js '); Circle.area (4); Circle.circumference (4);

---------------------

// using Module.exports to demonstrate the same functionality var PI =function(r) {  return  {    function() {       return PI * R * r;    },    function() {      return 2 * PI * r;   }}

// accessing the exposed functions in the node shell var circle = require ('./circle.js '); var mycircle = Circle (4); Mycircle.area (); Mycircle.circumference ();

To summarize this, the general thumb-rule is with the exports statement to export instances of modules. Use the Module.exports statement to export JavaScript objects.

[Node. js] Exporting Modules in Node

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.