Nodejs Learning---Module

Source: Internet
Author: User

Recently in the study Nodejs, the basic knowledge looked, and then ran to see Express, but really write code when the hands or not stable enough. This means that basic knowledge is not a tick, so return to re-study.

The most common use of NODEJS projects is the module, the declaration of the module, and the invocation of the module. But what exactly is a module? What are the ways that the claims are made, and what are the ways to invoke them? Go straight to the chase and see below

Let's start with the simplest one:

Main.js

Main.js var example = require ('./example '); Example.show ();

Example.js

Example.jsfunction Show () {Console.log ("Hi,i ' m Example");} Exports.show = Show;

OK, very simple, run: node main.js

The result shows that the call outputs the show () method in Example.js.

Look closely at the code, you will find that Main.js first introduced the Example.js module, and then assign to example this variable, the call is Example.show (), that is, the example attribute has show () this method. In fact, let's test it, rewrite the code.

Main.js

Main.jsvar example = require ('./example '); Example.show (); Console.log (example); Add a line of output

Results:

Look at the last line, the output of the variable example is a {key:value} such son of a guy, what is this? It's an object!

Tidy up.

Statement: Exports chestnut: exports.xxx = yyy;

Call: Require chestnut: var xxx = require (' yyy '); Get a {...} The object of such a child

OK, you can read the above should be the basic way to use.

Then we can write a few more function in the example.js, then exports.xxx = that function,main.js call. Although this is not a problem, but it is recommended that a JS file only to do a module to use, clear view of the project structure to know the function, so it is not better.

Go on, now, in a different way.

Example.js

Example.jsmodule.exports = function () {Console.log ("Hi,i ' m Example");}

Main.js

Main.jsvar example = require ('./example '); Example.show ();

Run directly to see the results:

It's an error! Show () is a undefined that shows that the example module we are calling does not have the show () property.

Output See what the example variable is.

Add the code in the Main.js to the direct output example:

Console.log (example);

Results:


Example becomes a function object, and through typeof you can see that it's a function.

This means that we pass the Module.exports = function () {...} to encapsulate an object directly into a module, then get the module by require and get an object directly.

So, how do you use this module? Look at the code:

Main.jsvar example = require ('./example '); var _example = new example ();

Results:

Bingo!

To summarize:

Mode 1: Create---exports.xxx = function () {...}

Use---var xxx = require (' yyy ');

Xxx.zzz ();

Mode 2: Create---module.exports = function () {...}

Use---var xxx = require (' path ');

var yyy = new xxx ();

So far as we can think of, the main point is that one is the method defined on the exports object, and the other is that the interface object is the object to be exported.

Follow the content slowly add it, first recorded, convenient for later review.

Nodejs Learning---Module

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.