Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript module pattern, nested functions, and sub modules

I am trying to wrap my head around javascript modules, but I'm unsure how to split up a module into further sub modules. I have read that nested functions are not really a great idea, due to performance, so how do I break up a function in a module? For example, lets say I have the following module:

var Editor = {};

Editor.build = (function () {
    var x = 100;
    return {
        bigFunction: function () {
            // This is where I need to define a couple smaller functions 
            // should I create a new module for bigFunction? If so, should it be nested in Editor.build somehow?
        }
    };
})();

bigFunction is only related to Editor.build. Should I attach the smaller functions that make up bigFunction to the prototype bigFunction object? I'm not even sure if that would make sense.

var Editor = {};

Editor.build = (function () {
    var x = 100;
    return {
        bigFunction: function () {
            bigFunction.smallFunction();
            bigFunction.prototype.smallFunction = function(){ /*do something */ };   
            // not sure if this even makes sense
        }
    };
})();

Can someone please throw me in the right direction here? There is so much misleading information online, and would just like a definite guide on how to deal with this sort of modularization.

Thank you.

like image 845
Errol Fitzgerald Avatar asked Nov 14 '22 03:11

Errol Fitzgerald


1 Answers

Here is a snippet I use to make names for an input:

    var dynamicCounter = 0;
    //custom dropdown names
    var createContainerNames = function () {
        function Names() {
            this.id = "Tasks_" + dynamicCounter + "__ContainerId";
            this.name = "Tasks[" + dynamicCounter + "].ContainerId";
            this.parent = "task" + dynamicCounter + "Container";
        }
        Names.prototype = { constructor: Names };
        return function () { return new Names(); };
    } ();

And then I use it:

    var createdNames = createContainerNames();
    var createdId = createdNames.id;
    dynamicCounter++;
    var differentNames = createContainerNames();
    var differentId = differentNames.id;

Another approach would be to do this:

var NameModule = function(){

 //"private" namemodule variables
 var priv1 = "Hello";

 //"private namemodule methods
 function privMethod1(){
  //TODO: implement
 }

 //"public namemodule variables
 var pub1 = "Welcome";

 //"public" namemodule methods
 function PubMethod(){
  //TODO: pub
 } 

 return {
  pub1 : pub1,
  PubMethod: PubMethod
 };

and then to use it

var myPubMethod = new NameModule();
myPubMethod.PubMethod();
var pubVar = myPubMethod.pub1;

EDIT

You could also take this approach:

var mod = function(){
 this.modArray = [];
};

mod.prototype = {

 //private variables
 modId: null,

 //public method
 AddToArray: function (obj) {
    this.modArray.push(obj);
 }
}
like image 103
Travis J Avatar answered Nov 16 '22 02:11

Travis J