1

I am writing a javascript framework using the module pattern, which looks like this:

var EF = (function(doc) {

    // Main public object
    var e = {
        version: "1.0.0",
        doc: doc,
        log: true
    };

    // Private methods

    var method1 = function(el) {
        <some code>
    }

    var method2 = function(el) {
        <some code>
    }

    var method3 = function(el) {
        <some code>
    }


    // PUBLIC METHODS ASSIGNMENT

    e.method1 = method1;
    e.method2 = method2;
    e.method3 = method3;

    return e;

}(document));

Now I decided that I should move some methods to the separate file. During development I then would load two files one by one in the HTML file, while for deployment I would merge them in a single file.

What is the correct way move part of the methods to a separate files keeping the structure of the code that I use?

I've seen the following suggestion in one of the answers on stackoverflow:

var MODULE = (function (my) {
    var privateToThisFile = "something";

    // add capabilities...

    my.publicProperty = "something";

    return my;
}(MODULE || {}));

It looks almost like what I need, but it shows private methods for the file and for the module. But I need private methods for the module and pubic methods for the module.

What suggestions would you have?

BartoNaz
  • 2,743
  • 2
  • 26
  • 42
  • 3
    All functions that need access to the same private methods need to reside in the same script file. – Bergi Aug 05 '13 at 16:06
  • That's cos they are not private methods but local variables referencing function objects, the metaphor breaks down pretty much right away – Esailija Aug 05 '13 at 16:14
  • see possible duplicate [How can I share module-private data between 2 files in Node?](http://stackoverflow.com/questions/17334081/how-can-i-share-module-private-data-between-2-files-in-node) for a possible pattern if just [dividing into smaller modules](http://stackoverflow.com/a/12483424/1048572) doesn't work for you – Bergi Aug 05 '13 at 16:18

1 Answers1

1

I am not sure what exactly you want, but I guess you want to access the private function via module object. In essence it's public pointer to a private property.

Let me know further if I can help you. A problem example with a jsFiddle would be great.

var myRevealingModule = function () {

    var privateCounter = 0;

    function privateFunction() {
        privateCounter++;
    }

    function publicFunction() {
        publicIncrement();
    }

    function publicIncrement() {
        privateFunction();
    }

    function publicGetCount(){
      return privateCounter;
    }

    // Reveal public pointers to 
    // private functions and properties        

   return {
        start: publicFunction,
        increment: publicIncrement,
        count: publicGetCount
    };

}();

myRevealingModule.start();
ʞɹᴉʞ ǝʌɐp
  • 5,350
  • 8
  • 39
  • 65