11

I'm new to node.js (and stackoverflow) and haven't found an exact explanation of this.

This is probably a trial answer but hopefully it'll help someone else who's also transitioning from Python/other object oriented frameworks.

I've seen other articles about what the prototype concept is in js and then others that explain the module.exports of node.js.

I'm studying the Ghost CMS and they use both. I can't seem to pick out why they would choose one over the other in certain cases.

Any help is appreciated, even if it's pointing me to other links.

Vatine
  • 20,782
  • 4
  • 54
  • 70
elloworld111
  • 255
  • 1
  • 5
  • 10
  • 3
    They have nothing whatsoever to do with eachother. `module.exports` is for importing other modules; prototypes are for creating instances. – SLaks Feb 21 '14 at 16:20
  • 1
    Thanks SLaks, I just thought that since you can access module elements through module.function that it was similar to prototype since you can reach a prototyped attribute the same way. – elloworld111 Feb 21 '14 at 16:47

2 Answers2

20

Actually they are interchangeable (in a way):

with prototype:

//module.js
function Person (name) {
  this.name = name;
}

Person.prototype.sayName = function () {
  console.log(this.name);
}

module.exports = Person;

//index.js
var Person = require('./module.js');
var person = new Person('John');

person.sayName();

with exports:

//module.js
exports.init = function (name) {
  this.name = name;
  return this;
}

exports.sayName = function () {
  console.log(this.name);
}

//index.js
var Person = require('./module.js');
var person = Object.create(Person).init('John');

person.sayName();

The first example is more idiomatic for javascript, though.

vkurchatkin
  • 13,364
  • 2
  • 47
  • 55
  • 4
    They're not really interchangeable at all. Interfacing with an object's prototype and interfacing with a (module) object's export property have very different typical use cases. It's misleading at best and possibly harmful to consider them interchangeable. JS developers should really understand the difference. – heyheyjp Feb 21 '14 at 18:32
  • Thanks for this answer, I was looking for something more specific though. I've started to use the prototypes where it makes sense thanks to you. – elloworld111 Feb 21 '14 at 18:34
  • 1
    @prattsj `exports` and `prototype` are really different things as you explained. I just wanted to show the way they can be used to do the same things. It's not the best answer for this particular question, just an addition. – vkurchatkin Feb 21 '14 at 18:54
10

With node.js, module.exports is how one exposes the public interface of a module.

/* my-module.js */

exports.coolFunction = function(callback) {

    // stuff & things
    callback(whatever);
};

This interface can then be consumed by another module after importing/requiring it:

/* another-module.js */

var myModule = require('my-module');

myModule.coolFunction(function(error) { ... });

Prototypes (a plain Javascript feature), on the other hand, are useful for defining shared properties and methods of objects instantiated from a constructor function.

function User() {
    this.name = null;
}

User.prototype.printGreeting = function() {
    console.log('Hello. My name is: ' + this.name);
};

var user = new User();
user.name = 'Jill';

user.printGreeting();

Cheers.

Community
  • 1
  • 1
heyheyjp
  • 626
  • 4
  • 8