Several modules loading modes of Felayman---Nodejs

Source: Internet
Author: User

Several modules loading modes of Nodejs

I. Adding a method directly to the exports object

1. First create a module (module.js) module.js

Exports. one = function () {console.log (' first module ');};
2.load.js

var module  =require ('./module '), module. One ();
This allows us to return a exports object after the module is introduced, which refers to the module object, in fact, only two references or handles, but all point to the same resource, in Load.js, the module name can be arbitrarily taken, Because it is simply pointing to require ('./module '); Returns a reference to an instance object, the module in the Load.js file and the exports object in Module.js are the same thing. Therefore, the above two files can be represented by a file:

Exports. one = function () {console.log (' first module ');}; Exports. One ();
The results are consistent, and here we can see very clearly that we are actually returning after using require ('./xxxx ')The reference to the exports object in the Xxxx.js file, the name of the reference can be arbitrarily taken, but in order to standardize we still best to sign some non-standard provisions (said later), but this will be inappropriate, because it is always point to exports instance object, that is, Although we have this module, we can only use this module once, depending on the Rquire ('./module ') only once the module is added. For example, if we modify the above code,

Module.js

var name; exports.setname = function (oname) {name = Oname;}; Exports.getname  = function () {console.log (name);};

Load.js

var module1  = require ('./module '); Module1.setname ("Felayman1"); Module1.getname (); var module2  = require ('./ Module '), Module2.setname ("felayman2"); Module2.getname (); Module1.getname ();
We can see that although we have used two times require ('./module '), but when we modify the Module2, the contents of Module1 are also modified, which precisely explains Module1 and Module2 are the same objects that point to. Sometimes this doesn't affect our program, but what if our module is a person? We want to return a different object after we require ('./person '). So this approach is flawed, Although convenient, this approach is common in most nodejs modules, such as FS modules, HTTP modules, and so on.

Two. Mount the functions in the module to the properties of the exports object

Person.js

<span style= "Font-family:courier new;font-size:18px;" >function person{<span style= "WHITE-SPACE:PRE; "></span>var name;<span style=" WHITE-SPACE:PRE; "></span>this.setname = function (thename) {<span style=" white-space:pre; "></span>name = Thename;<span style=" WHITE-SPACE:PRE; "></span>};<span style=" WHITE-SPACE:PRE; "></span>this.sayhello = function () {<span style=" white-space:pre; "></span>console.log (' Hello ', name); <span style=" WHITE-SPACE:PRE; "></SPAN>};} Exports. person = Person;</span><span style= "font-size:24px;font-family: ' Microsoft Yahei '; "></span>
Load.js
var person  = require ('./person '). Person;var Person1 = new Person ();p erson1.setname ("Felayman1");p Erson1.sayhello (), var person2 = new Person (); Person2.setname ("felayman2");p Erson2.sayhello ();p Erson1.sayhello ();



So we can see that we can introduce a function, we set the person function in the Person.js file to a property of the Eports object, we only need to introduce the property in the Load.js file, we can get to more than one instance of the function, The Eventemitter in Nodejs is based on this approach, but in this way we are always using require ('./person '). This is a bit too complex to be written, so Nodejs allows us to use the global variable--module in other, more concise ways, which makes it more convenient for us to introduce other modules in other files.


Three. Using the global variable module

Person.js

<span style= "Font-family:courier New;" >function person () {var name;this.setname = function (thename) {name = thename;}; This.sayhello = function () {console.log (' Hello ', name);};} Exports. person = Person;module.exports = person;</span>
Load.js

var person  = require ('./person '), var person1 = new Person ();p erson1.setname ("Felayman1");p Erson1.sayhello (); var Person2 = new Person ();p erson2.setname ("felayman2");p Erson2.sayhello ();p Erson1.sayhello ();
Such a modification, we are in the use of the Require function when it is convenient, if it is difficult to understand here, we can put the syntax of two files together:

var person  = require ('./person ');
Module.exports = person;
So, we can see that this is actually the case
var person = person  .  

Because we have already said, require ('./person ') is actually module.exports object, here the module we do not care about, as in JavaScript window, is a global variable, that is, Module.exports =exports is similar to the effect of Window.alert () =alert (), so that we can see that we use require again ('./person ') is actually importing the property function template of the exports object we need, so that we can instantiate the object we need more than once. This approach is a combination of the first two methods, and therefore is the official recommended method of use.















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.