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());