0

I am learning how to split my program across several javascript files that can be used in a browser – see my code below. I want to access the private variables and methods in MODULE while inside MODULE.Submodule (or vice versa). The obvious answer is that I should make those variables and methods public in MODULE. Of course, if I make them public, then they are no longer private. When I had all my code in one single file, then the methods inside what is now MODULE.SubModule could access all the private variables. I want to replicate that capability. That is, the private vars and methods in each module remain private to all the modules (or better, to specific modules) and only the public vars and methods from all modules are made public.

I've found several tutorials on how to achieve the splitting, but none seem to address this specific problem – accessing private vars and methods in one module while in another module in the browser.

// in module.js

let MODULE = (function () {

    const privateVar = 'foo';

    return {
        publicVar: 'bar',

        // should return foo -> works
        publicMethod: function () { return privateVar; };
    };

})();

// in module-submodule.js
MODULE = (function (my) {

    const privateVar = 'baz';

    my.SubModule = {

        // should return baz -> works
        publicMethod: function () { return privateVar },            

        // should return foo from parent MODULE -> DOES NOT work
        superPublicMethod: function() { return my.privateVar}
    }

    return my;

})(MODULE || {});

console.log(MODULE.publicMethod()); // return 'foo'
console.log(MODULE.SubModule.publicMethod()); // returns 'baz'
console.log(MODULE.SubModule.superPublicMethod()); // returns 'undefined'
punkish
  • 13,598
  • 26
  • 66
  • 101
  • 1
    I believe you need to rethink your requirements. The whole point of private variables is that they are not available from the outside. That you are writing both pieces is mostly irrelevant. Anything that needs to be available to other parts of your codebase needs some sort of public access. – Scott Sauyet May 28 '19 at 20:08
  • of course, there is that :) I concede your point, and that is what I implied with my *Of course, if I make them public, then they are no longer private* comment. But I am hoping to find a way to make *the private vars and methods in each module remain private to all the modules (or better, to specific modules) and only the public vars and methods from all modules are made public.* – punkish May 28 '19 at 20:10
  • I don't think you'll be able to entirely achieve that. But you might approximate it using [Symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)s. – Scott Sauyet May 28 '19 at 20:17
  • It sounds like what you want is a [*protected*](https://stackoverflow.com/q/1020749/1715579) or perhaps [*friend*](https://en.wikipedia.org/wiki/Friend_function) access. This is going to be pretty difficult to achieve because JavaScript doesn't really have all the expressiveness of more object-oriented languages like Java or C++. I'd recommend sticking to an idiomatic coding style and just using public (accessible to anyone) or private (accessible only within the current module) members, and use names like `_foo` to indicate a public member that is nevertheless intended only for internal use. – p.s.w.g May 28 '19 at 22:42

0 Answers0