3

As I am new to Node.js I am looking for some info, coding some testing stuff and also reading some other people's code.

I've seen that creating/requiring(using) modules is typical in Node.js. I've seen different ways of defining "public" methods and function inside modules and both seem to work the same way too:

  • module.exports
  • this.prototype

Is there a notable difference by using one or another? Or are just different ways of doing the same thing? Is any of these two better, or it depends on the context?

charliebrownie
  • 5,777
  • 10
  • 35
  • 55
  • 2
    Never once seen `this.prototype` used to define a module. You should always follow the methods defined in the [documentation](http://nodejs.org/api/modules.html). – Ben Fortune Jan 15 '15 at 16:38
  • @BenFortune I found `this.prototype` while reading some other people's code. Have a look to [this code](https://github.com/ttezel/twit/blob/master/examples/bot.js) – charliebrownie Jan 15 '15 at 16:47
  • There is no `this.prototype` in the code you linked to... – mscdex Jan 15 '15 at 16:53
  • 1
    @BenFortune also found this too the other day: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work – charliebrownie Jan 15 '15 at 16:54
  • @mscdex there is... lines: 14, 26, 49 – charliebrownie Jan 15 '15 at 16:54
  • 1
    @charliebrownie There is a huge difference between `this.prototype` and `variable.prototype`. – Ben Fortune Jan 15 '15 at 16:59
  • @BenFortune what is the difference? – charliebrownie Jan 15 '15 at 17:03
  • 1
    @charliebrownie In the case of `variable.prototype`, the `variable` will normally refer to a "constructor" `function`. The `prototype` is the object that each `new` instances of that constructor will inherit properties from. [How does JavaScript .prototype work?](http://stackoverflow.com/a/4778408) – Jonathan Lonowski Jan 15 '15 at 17:17
  • @JonathanLonowski so `module.exports` is used to define some properties that every instance of that module will inherit, and `variable.prototype` is to dynamically add/create new functionality *on the fly* to any instance... am I right? – charliebrownie Jan 16 '15 at 12:30

1 Answers1

2

You should use either exports to attach properties to the predefined exported object or reassign module.exports to your own object. The latter is common when exporting a constructor for example.

exports.foo = function() { console.log('Hello world!'); };
exports.add = function(a, b) { return a + b; };

// Then the module might be used like so:
// var mymodule = require('./mymodule');
// mymodule.foo();
// console.log(mymodule.add(1, 9));

Or replace the exports object:

function Foo() {

}

module.exports = Foo;

// then typically users do this in their script:
// var Foo = require('./mymodule');
// var myFoo = new Foo();
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • 1
    Yes, `module.exports` and `exports` are just the same thing right? They are cool explained [here](http://www.sitepoint.com/understanding-module-exports-exports-node-js/)! – charliebrownie Jan 15 '15 at 16:51
  • 1
    It's `module.exports`, but yes, they are *initially* the same thing. However `module.exports` allows you to override the exported object as shown in my examples. – mscdex Jan 15 '15 at 16:52
  • You are right, I spelled it wrong, thanks for pointing it out. Just edited the whole thing. Thanks! – charliebrownie Jan 15 '15 at 16:57