2

Say I have a module in Node that has two functions func1() and func2(). Func1 needs to make a call to func2 during its execution.

I want to have func2 in the exports so it can be individually tested, so I am setting the exports module exports to include both func1 and func2:

(function (module) {

    module.func1= function (something) {

        var result = module.func2(something);

        return result + something;    
    }

    module.func2 = function(something) {

        return something * something;
    }
})(module.exports);

The question is.. Is this the best way of defining / calling func2?

I can't think of any other way of doing it but making a call to module.func2() just seems a little wrong to me for some reason.

Update: to elaborate further:

It seems that by using module.func2 I am really calling module.exports.func2 which is going out of the class to come back in again rather than keeping it all internal..

No other reason really, I appreciate that this works, I was just wondering if this was the generally accepted setup in Node

0

1 Answer 1

1

You can use this instead of module, like this

....
var result = this.func2(something);
...
Sign up to request clarification or add additional context in comments.

1 Comment

great, that seems much more sensible!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.