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 c
This question doesn't really have anything to do with how require() works. Basically, whatever you set module.exports to in your module will be returned from the require() call for it.
This would be equivalent to:
var square = function(width) {
return {
area: function() {
return width * width;
}
};
}
There is no need for the new keyword when calling square. You aren't returning the function instance itself from square, you are returning a new object at the end. Therefore, you can simply call this function directly.
For more intricate arguments around new, check this out: Is JavaScript's "new" keyword considered harmful?