简体   繁体   中英

What are node.js modules?

Regarding this question: What is the purpose of Node.js module.exports and how do you use it?

I'm a Javascript beginner. In the referenced question...

mymodule.js code

var myFunc = function() { ... };
exports.myFunc = myFunc;

main js file

var m = require('./mymodule.js');
m.myFunc();

Is mymodule essentially a class file defining objects?

Node.js allows code to be separated into different modules. This modules are just javascript files that can expose functions or objects using the exports object.

  1. There are more details of this convention
  2. Nice documentation of the Node.js modules

There are no Classes in JavaScript but you can use patterns to emulate that behaviour. There is a question about implementing OOP patterns in JavaScript: what pattern to use when creating javascript class?

As a beginner there are very good books for JavaScript:

  1. JavaScript: The Good Parts
  2. JavaScript Patterns

They are short and will give you a very good insight of the JavaScript Language.

Is mymodule essentially a class file defining objects?

and functions, although in Javascript functions are objects, so the distinction may be moot.

Importantly, each module has its own scope, so any var declared therein will not be visible outside of the module.

The rest of your question about users and lists doesn't make sense as written. Javascript OO programming is a complete topic in its own right, and the module system doesn't really change that. Modules are just a way of wrapping code libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM