繁体   English   中英

学习在Javascript中使用名称空间和模块

[英]Learning to use namespaces and modules in Javascript

因此,我在jsfiddle中有一个需要帮助的示例。 我有一个html形式的表格,以及我想用来填充的数据。 我将执行此操作的逻辑封装在我认为是正确的名称空间定义中,但是JsHint认为不是,是的,我的数据未在表单控件中呈现。 希望能为您解决这个问题提供一些帮助。 绝对不使用Jquery,因为我为此仅使用Ext.js库。 提前致谢。

[JSFiddle链接到我的示例] [1]

 var data = {
        Tasks = [{
            "taskid": 1,
                "author": "Bill Maher",
                "description": "Purple Rain purple",
                "dateCreated": "12/23/2013",
                "dataModified": "2/23/2013",
                "dueDate": "2/30/2014",
                "status": "in progress"
        }, {
            "taskid": 2,
                "author": "Seth Green",
                "description": "I am not certain this is needed",
                "dateCreated": "3/24/2011",
                "dataModified": "",
                "dueDate": "5/3/2011",
                "status": "completed"
        }, {
            "taskid": 3,
                "author": "Arnold Schwatsheneggar",
                "description": "I would of course like to have data to test with",
                "dataModified": "",
                "dueDate": "",
                "status": "in progress"
        }, {
            "taskid": 4,
                "author": "Lilly blue",
                "description": "make the sun shine high again",
                "dateCreated": "4/12/2014",
                "dataModified": "",
                "dueDate": "5/10/2014",
                "status": "started"
        }, {
            "taskid": 5,
                "author": "Sam Raj",
                "description": " when will I see you again",
                "dateCreated": "",
                "dataModified": "",
                "dueDate": "",
                "status": "in progress"
        }, {
            "taskid": 6,
                "author": "Kate Kurtmann",
                "description": "Show me that you love me. See ya!",
                "dateCreated": "",
                "dataModified": "",
                "dueDate": "",
                "status": "in progress"
        }, {
            "taskid": 7,
                "author": "Kristen BenBazza",
                "description": "I am a real American",
                "dateCreated": "3/4/2013",
                "dataModified": "12/3/14",
                "dueDate": "5/23/2014",
                "status": "in progress"
        }, {
            "taskid": 8,
                "author": "Venkat Shack",
                "description": "You are the bravest of hearts, you are the strongest of souls",
                "dateCreated": "12/1/2001",
                "dataModified": "12/12/2003",
                "dueDate": "7/6/2003",
                "status": "started"
        }, {
            "taskid": 9,
                "author": "Sunny Chan",
                "description": "WHat the f is FADs anyway",
                "dateCreated": "12/1/2011",
                "dataModified": "3/12/2013",
                "dueDate": "10/10/2014",
                "status": "completed"
        }, {
            "taskid": 10,
                "author": "William Rolloff",
                "description": "Accounting for the costs improving care",
                "dateCreated": "2/12/2013",
                "dataModified": "12/01/2014",
                "dueDate": "10/15/2015",
                "status": "completed"
        }, {
            "taskid": 11,
                "author": "Aakash Khandari",
                "description": "Making a move to a better life and career",
                "dateCreated": "4/3/2000",
                "dataModified": "4/7/2005",
                "dueDate": "7/17/2014",
                "status": "in progress"
        }

        ]
    };

// more code goes here but has been deleted for brevity

   //revealing public API
    return {
        exporter.tracy: {//namespace definition 
            data = data,
            trainingTask: {//second namespace
                add = addTask,
                update = UpdateTask,
                load = loadDetail,
                clearDetail = clearForm,
                save = SubmitTask,
                remove = deleteRecord,
                expandGroup = groupexpand,
                collapseGroup = groupcollapse,
                toggleGroup = toggleGroup,
                fillMenu = fillMenu,
                setGroupStyle = setGroupStyle,
                isGrouped = isGrouped
            };
        };
    };
};
/*ending of the module*/
}(this)); //close tracy.trainingtask

您将在闭包(自执行的匿名函数)中声明和定义TaskSetJson ,这对于模块化代码是正确的。

但是您忘记了将其公开。

您可以使用RMP(显示模块模式),如下所示:

tracy = (function() {
  var foo = function(a,b,c) { ... };
  var var = function(e,f,g) { ... };
  var private_value = 1;
  var public_value = 2;
  // "Reveal" the public parts of your module
  return {
    foo: foo,
    var: var,
    public_value: public_value
  };
})();

通过这种模式,您的变量和函数在闭包(匿名函数)中声明。 因此,它们仅在该闭包内部可用。 但是您可以通过返回它们使它们在闭包之外可用。

如果要在保存var的地方扩展现有的全局变量,而不是返回模块的显示部分,则可以将其传递给自执行函数,然后直接附加到它:

(function(tracy) {
  var foo = function(a,b,c) { ... };
  var var = function(e,f,g) { ... };
  var private_value = 1;
  var public_value = 2;
  // "Reveal" the public parts of your module
  tracy = {
    foo: foo,
    var: var,
    public_value: public_value
  };
})();

这两个示例有些不同,但是在两种情况下,您都可以调用tracy.footracy.var或访问tracy.public_value

第一个示例更加灵活,因为您可以获得模块的几个独立实例,并将它们存储在不同的变量中。

var声明一个变量,可以选择将其初始化为一个值。 报价mdn

用var声明的变量的范围是其当前的执行上下文,它是封闭的函数,或者对于在任何函数外部声明的变量,是全局函数。

在您的jsfiddle中,您似乎期望可以访问在函数内部定义的变量,但这是不可能的。

我本来要拨弄你的小提琴的,但是它很大。 这是一个使用“命名空间”的小例子,请记住,这样做的目的是不污染全局范围

(function(export) {

  var some_private_variable;
  var some_other_private_variable;

  function loadDetail () {

  }

  var taskSet = [{
    //a task  
  }];


  export.Tracy = {
    trainingTask: {
      load: loadDetail,
      add: addTask
    },
    TaskSet: taskSet
  };

})(this);

由于this是执行时的全局对象(窗口),因此export是窗口对象。 因此,您可以稍后按以下方式使用它:

 Tracy.trainingTask.load(..)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM