2

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?

Community
  • 1
  • 1
gotta have my pops
  • 878
  • 4
  • 11
  • 22

2 Answers2

4

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.

Community
  • 1
  • 1
eliocs
  • 18,511
  • 7
  • 40
  • 52
  • The two links "The Good Parts", "Patterns" are not found. – vik Jul 15 '12 at 01:08
  • URI behind the Link is `http://rads.stackoverflow.com/amzn/click/0596806752` I can not even open any connection to rads.stackoverflow.com – vik Aug 08 '12 at 08:40
2

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495