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

Learning to use namespaces and modules in Javascript

本文关键字:空间 模块 Javascript 学习      更新时间:2023-09-26

因此,我在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(..)