Sharing private methods across files in JavaScript Module Pattern design

119 views Asked by At

I'm creating a JavaScript library and I'm trying to split its functionality across two files.

Here's a simplified summary of what I have right now, in a file named main.js:

module.exports = (function() {
    // PRIVATE
    var _sendRequest = function(callback) {
        // send request and get response
        callback(response);
    };

    // PUBLIC
    var getInfo = function() {
        _sendRequest(function(response) {
            console.log("Response: ", response);
        );
    }

    return {
        getInfo: getInfo
    };
})();

As you can see, only getInfo is publicly accessible. _sendRequest is hidden away for private usage.

What I'm trying to do is put the _sendRequest method in a separate file while still keeping it private to the outside. I only want getInfo to be publicly accessible.

Is there a way to do this with the Module Pattern design, or should I think about switching design patterns? I've looked into Augmented Module Patterns, but they don't seem to keep methods like _sendRequest private as they should be. I just want to store all my private utilities in a separate file while maintaining their private state. Is this possible?

0

There are 0 answers