I've been interested in prototypical programming with JavaScript, and I'm trying to figure out an efficient way of doing it with Node modules.
For example, I'd like to use a prototype to quickly create a debug
object in each of my modules, which has a name
property and a log
method, constructed via:
custom_modules/debug.js
var settings = require('custom_modules/settings');
exports = function debug(name){
this.name = name;
this.log = function(message){
if (settings.debug == 'true'){
console.log("[Debug][" + name + "]: " + message);
}
}
}
So I'd like to know if I can use that module as a constructor, like so:
do_something.js
var debug = new require('custom_modules/debug')("Something Doer");
debug.log("Initialized"); // -> [Debug][Something Doer] : Initialized
Will it work? If not, what's the correct equivalent?