You don't actually need a new object (exports already is one), but you do 
need to make sure within your module you reference exports's attribute that 
points to the function, rather than referencing the function by name, e.g.

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

exports.doSomethingElse = function () {
   // doSomething();  // this will always call the local function
   exports.doSomething();  // calls the exported function, which can be 
overwritten
}

In practice if doSomething is a function that generally you'd expect to be 
private to the module and only need to override under special
circumstances I'd recommend either giving it a weird prefix (e.g. 
_doSomething) or embedding it in a sub-object, e.g.

var private = {
   doSomething: function () { ... }
};
exports.private = private; // export private methods for testing

exports.doSomethingElse = function () { private.doSomething(); }

Then when you're overriding it it will be more obvious that this is an odd 
thing to do

var myMod = require("myMod);

myMod.private.doSomething = function () { ... }; // override private 
doSomething method

If you wanted to get fancy you could even make the private export 
conditional on environment e.g.

if (process.env.NODE_ENV === "development") {
   exports.private = private; // export private methods for testing
}

On Tuesday, March 10, 2015 at 8:14:41 AM UTC-7, zladuric wrote:
>
> Your module holds internally to the reference of the internal function. 
> Furthermore, it doesn't even know about the external one.
>
> So it means your doSomethingElse needs to address the proper function 
> reference, like override the prototype or use some closure object.
>
> You can try something like this:
>
> `myMod.js`
>
>     function doSomething() {
>
>        console.log('internal');
>     }
>     function callSomething() {
>         obj.doSomething();
>     }
>     var obj = {
>
>        doSomething: doSomething,
>        callSomething: callSomething
>     };
>
>     module.exports = obj;
>
>
> Module usage:
>
>     zlatko@localhost:~/tmp$ node
>     > var m = require('./mod')
>
>     > m.doSomething()
>     internal
>
>     > m.callSomething()
>     internal
>
>     > m.doSomething = function() {console.log('external');}
>     [Function]
>     > m.callSomething()
>     external
>
>
> The alternative would be that you override the module prototype once you 
> load it.
>
> Zlatko
>
>
> On Monday, March 9, 2015 at 4:39:58 AM UTC+1, MC wrote:
>>
>> Hello,
>> I'm trying to override function implementation where original function is 
>> part of nodejs module (myMod.js):
>> 'use strict';
>> function doSomething(req,res) {
>>  console.log('internal');
>> }
>> function doSomethingElse(req,res) {
>>  doSomething(req,res);
>> }
>> module.exports.doSomething=doSomething;
>>
>> I can override doSomething from the outside:
>> var myMod = require('myMod.js');
>> myMod.doSomething = function(req,res) {
>>  console.log('external');
>> };
>>
>> And when I call doSomething new implementation is used (prints 
>> 'external').
>> But when I call doSomethingElse it still prints 'internal' - it looks 
>> like inside the module original implementation is used.
>>
>> I saw a post that suggested declaring a variable (inside module):
>> var doSomehing = module.exports.doSomething = doSomething;
>>
>> But using that didn't help.
>> Any suggestions on how to replace doSomething so that calling 
>> doSomethingElse prints 'external'?
>> Thanks,
>>
>> M
>>
>>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/5f9e27f1-7449-4647-ba6f-35995fa81345%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to