gotta have my pops
gotta have my pops

Reputation: 898

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?

Upvotes: 2

Views: 622

Answers (2)

eliocs
eliocs

Reputation: 18827

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.

Upvotes: 4

Alnitak
Alnitak

Reputation: 339816

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.

Upvotes: 2

Related Questions