Take this for example:
function SomeClass() {}
SomeClass.prototype = {
sayHi: function() {
console.log('hello');
}
};
function foo() {
return new SomeClass();
}
foo() // => returns a new SomeClass object
In that case, foo
is what they're referring to as a "factory" function, in that it is sort of like a type of factory that creates objects (just like in real life how factories create new objects; car factories, etc.)
It's also good for singletons:
(function() {
var singleton;
function SomeClass() {}
SomeClass.prototype = {
sayHi: function() {
console.log('hello');
}
};
function foo() {
singleton = singleton || new SomeClass();
return singleton;
}
window.foo = foo;
}());
foo(); // returns a new SomeClass
foo(); // returns the same SomeClass instance (not a new one). This is a singleton.
Node.js is just regular JavaScript, so in the example you've given, all you're doing is including the factory from a module and using it to create new objects (instances of "classes").