Node.js - use of module.exports as a constructor

Naresh

According to the Node.js manual:

If you want the root of your module's export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to module.exports instead of exports.

The example given is:

// file: square.js
module.exports = function(width) {
  return {
    area: function() {
      return width * width;
    }
  };
}

and used like this:

var square = require('./square.js');
var mySquare = square(2);
console.log('The area of my square is ' + mySquare.area());

My question: why does the example not use square as an object? Is the following valid and does it make the example more "object oriented"?

var Square = require('./square.js');
var mySquare = new Square(2);
console.log('The area of my square is ' + mySquare.area());
Sukima

CommonJS modules allow two ways to define exported properties. In either case you are returning an Object/Function. Because functions are first class citizens in JavaScript they to can act just like Objects (technically they are Objects). That said your question about using the new keywords has a simple answer: Yes. I'll illustrate...

Module exports

You can either use the exports variable provided to attach properties to it. Once required in another module those assign properties become available. Or you can assign an object to the module.exports property. In either case what is returned by require() is a reference to the value of module.exports.

A pseudo-code example of how a module is defined:

var theModule = {
  exports: {}
};

(function(module, exports, require) {

  // Your module code goes here

})(theModule, theModule.exports, theRequireFunction);

In the example above module.exports and exports are the same object. The cool part is that you don't see any of that in your CommonJS modules as the whole system takes care of that for you all you need to know is there is a module object with an exports property and an exports variable that points to the same thing the module.exports does.

Require with constructors

Since you can attach a function directly to module.exports you can essentially return a function and like any function it could be managed as a constructor (That's in italics since the only difference between a function and a constructor in JavaScript is how you intend to use it. Technically there is no difference.)

So the following is perfectly good code and I personally encourage it:

// My module
function MyObject(bar) {
  this.bar = bar;
}

MyObject.prototype.foo = function foo() {
  console.log(this.bar);
};

module.exports = MyObject;

// In another module:
var MyObjectOrSomeCleverName = require("./my_object.js");
var my_obj_instance = new MyObjectOrSomeCleverName("foobar");
my_obj_instance.foo(); // => "foobar"

Require for non-constructors

Same thing goes for non-constructor like functions:

// My Module
exports.someFunction = function someFunction(msg) {
  console.log(msg);
}

// In another module
var MyModule = require("./my_module.js");
MyModule.someFunction("foobar"); // => "foobar"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

module.exports vs. export default in Node.js and ES6

From Java

Declare multiple module.exports in Node.js

From Java

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

From Java

module.exports vs exports in Node.js

From Dev

Node.JS module.exports for passing parameter between two functions?

From Dev

What's the use of exports / module.exports?

From Dev

Node module.exports pattern - Is this correct?

From Dev

How to reference a variable inside a module.exports in Node.js

From Dev

Different module.exports pattern in node.js

From Dev

Async readFile module.exports in node.js

From Dev

meaning of module.exports= function in node.js

From Dev

Node JS call a "local" function within module.exports

From Dev

Different results using Node.js' module.exports in Yeoman

From Dev

Node.js module.exports not exporting the whole object

From Dev

Node module.exports returns undefined

From Dev

Node.js/MongoDB: How can I use module.exports to pass a localhost database url to server.js file?

From Dev

Node js object exports

From Dev

Using 'this' in object method in node module.exports

From Dev

Node.js module.exports change principal variable

From Dev

Use return in constructor for wrapping node module

From Dev

meaning of module.exports= function in node.js

From Dev

Getting a variable via module.exports in node

From Dev

Node.js : sharing variables between modules via module.exports

From Dev

Consolidate multiple callbacks in node.js module exports

From Dev

Node.js - Impact of declaring module.exports twice for WebStorm intellisense

From Dev

Node.js Express module exports

From Dev

Node.js in module.exports my var is underfined

From Dev

What does Node.js 'require()' return if there is nothing assigned to module.exports

From Dev

How to use module.exports properly in node.js?

Related Related

  1. 1

    module.exports vs. export default in Node.js and ES6

  2. 2

    Declare multiple module.exports in Node.js

  3. 3

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

  4. 4

    module.exports vs exports in Node.js

  5. 5

    Node.JS module.exports for passing parameter between two functions?

  6. 6

    What's the use of exports / module.exports?

  7. 7

    Node module.exports pattern - Is this correct?

  8. 8

    How to reference a variable inside a module.exports in Node.js

  9. 9

    Different module.exports pattern in node.js

  10. 10

    Async readFile module.exports in node.js

  11. 11

    meaning of module.exports= function in node.js

  12. 12

    Node JS call a "local" function within module.exports

  13. 13

    Different results using Node.js' module.exports in Yeoman

  14. 14

    Node.js module.exports not exporting the whole object

  15. 15

    Node module.exports returns undefined

  16. 16

    Node.js/MongoDB: How can I use module.exports to pass a localhost database url to server.js file?

  17. 17

    Node js object exports

  18. 18

    Using 'this' in object method in node module.exports

  19. 19

    Node.js module.exports change principal variable

  20. 20

    Use return in constructor for wrapping node module

  21. 21

    meaning of module.exports= function in node.js

  22. 22

    Getting a variable via module.exports in node

  23. 23

    Node.js : sharing variables between modules via module.exports

  24. 24

    Consolidate multiple callbacks in node.js module exports

  25. 25

    Node.js - Impact of declaring module.exports twice for WebStorm intellisense

  26. 26

    Node.js Express module exports

  27. 27

    Node.js in module.exports my var is underfined

  28. 28

    What does Node.js 'require()' return if there is nothing assigned to module.exports

  29. 29

    How to use module.exports properly in node.js?

HotTag

Archive