Nodejs Module Reference

Source: Internet
Author: User

The reference to the module is a very important part of the backend language, so how do you do that in Nodejs?

When referencing other modules, there are two common methods: Exports,module.exports.

Next, we'll write a demo to tell the difference.

Testmodule.js:

function User (name,title,post) {    this. name=name;      this. title=title;     this. post=function() {    console.log (' hello ' +this. name);}; Module.exports=user;

Testexports.js:

exports.sayhello=function(name) {    console.log (' Hello, ' +name);}

Test.js:

var testmodule=require ('./testmodule '); Console.log (typeofvar newtestobj=New testmodule (' Mike ', ' Zhejiang ', ' 311301 '); Console.log(typeof( Newtestobj)); var testexports=require ('./testexports '); Console.log (testexports);

Run the Test.js and output in turn:

function
Object
{sayhello: [Function]}

It is obvious that Module.exports is actually returning a constructor, and exports only returns an object.

Nodejs Module Reference

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.