Javascript模块模式,嵌套函数和子模块

Err*_*ald 5 javascript module

我试图围绕javascript模块,但我不确定如何将模块拆分为更多的子模块.我已经读过,由于性能的原因,嵌套函数并不是一个好主意,那么如何在模块中分解函数呢?例如,假设我有以下模块:

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?
        }
    };
})();
Run Code Online (Sandbox Code Playgroud)

bigFunction只与Editor.build有关.我应该将构成bigFunction的较小函数附加到原型bigFunction对象吗?我甚至不确定这是否有意义.

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
        }
    };
})();
Run Code Online (Sandbox Code Playgroud)

有人可以把我拉向正确的方向吗?网上有如此多的误导性信息,并且只是关于如何处理这种模块化的明确指南.

谢谢.

Tra*_*s J 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(); };
    } ();
Run Code Online (Sandbox Code Playgroud)

然后我使用它:

    var createdNames = createContainerNames();
    var createdId = createdNames.id;
    dynamicCounter++;
    var differentNames = createContainerNames();
    var differentId = differentNames.id;
Run Code Online (Sandbox Code Playgroud)

另一种方法是这样做:

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
 };
Run Code Online (Sandbox Code Playgroud)

然后使用它

var myPubMethod = new NameModule();
myPubMethod.PubMethod();
var pubVar = myPubMethod.pub1;
Run Code Online (Sandbox Code Playgroud)

编辑

您也可以采取这种方法:

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

mod.prototype = {

 //private variables
 modId: null,

 //public method
 AddToArray: function (obj) {
    this.modArray.push(obj);
 }
}
Run Code Online (Sandbox Code Playgroud)