0

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?

  • 1
    You're using `new` the wrong way in your example. Instead of creating new `debug` instance, you're trying to create new class instance using `require` function as a constructor. See [this question](http://stackoverflow.com/questions/17604497/how-does-require-work-with-new-operator-in-node-js) for more info. – Leonid Beschastny Jul 18 '14 at 19:31
  • Ah, that's right. I've fixed it. @LeonidBeschastny –  Jul 18 '14 at 20:57
  • Actually, since Felix noted it in his answer, I removed the fix to keep that answer consistent with the Q. @LeonidBeschastny –  Jul 18 '14 at 21:13

1 Answers1

1

new doesn't care where the function comes from. So yes, the function can be the result of requireing a module.

However, the module must directly export the function. In your current code you are merely assigning a new value to the local exports variable, and we all know that assigning to a local variable doesn't have any effect outside of its scope.

The module will still export an empty object. You have to override the exports property of the module:

module.exports = function() {...};

As pointed out, there will be problems with precedence, so you would have to do

var debug = new (require('custom_modules/debug'))("Something Doer");
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Actually, [check out this answer.](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-node-js-module-exports-and-how-do-you-use-it). The local `exports` variable initially acts as an alias to `module.exports`, so unless you overwrite it, it looks like you can use it the same way as module.exports. I'm no pro at using modules in Node yet, but it's what that answer states. –  Jul 18 '14 at 19:23
  • 2
    Yes, `module.exports === exports` initially (I never said it isn't). But if you assign a new value to `exports`, then `module.exports !== exports`. And the value that `require` returns is the value of `module.exports`, not the local variable `exports`. Here is a simplified example: `var foo = {bar: 42}; var bar = foo.bar; bar = 21; console.log(foo.bar, bar);`. Note that `foo.bar` still has its original value. – Felix Kling Jul 18 '14 at 19:25