Javascript module template, nested functions and helper modules

I'm trying to wrap my head around javascript modules, but I'm not sure how to split the module into additional submodules. I read that nested functions are not really a great idea because of performance, so how can I split a function in a module? For example, let's 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 applies only to Editor.build. Should I attach the smaller functions that make up the larger function for the prototype bigFunction object? I'm not even sure that this will 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 drop me in the right direction here? There is so much misleading information on the Internet, and it will be just as a concrete guide to how to deal with such modulation.

Thank.

+5
1

, :

    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(); };
    } ();

:

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

:

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
 };

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

:

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

mod.prototype = {

 //private variables
 modId: null,

 //public method
 AddToArray: function (obj) {
    this.modArray.push(obj);
 }
}
0

All Articles