mikedavies-dev
mikedavies-dev

Reputation: 3345

NodeJS module function convention - calling own functions

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

Upvotes: 2

Views: 160

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

You can use this instead of module, like this

....
var result = this.func2(something);
...

Upvotes: 1

Related Questions