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/b2c86f2e-daeb-4a08-a0fe-3352794c7336%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.