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?

3
  • 3
    All functions that need access to the same private methods need to reside in the same script file. Commented Aug 5, 2013 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 Commented Aug 5, 2013 at 16:14
  • see possible duplicate How can I share module-private data between 2 files in Node? for a possible pattern if just dividing into smaller modules doesn't work for you Commented Aug 5, 2013 at 16:18

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

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.