gpt4 book ai didi

javascript - 学习在 Javascript 中使用命名空间和模块

转载 作者:行者123 更新时间:2023-12-03 12:44:34 25 4
gpt4 key购买 nike

所以,我在 jsfiddle 中有一个示例需要一些帮助。我有一个 html 表单,以及我想填充它的数据。我已将执行此操作的逻辑封装在 namespace 定义中,我认为这是正确的,但 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.

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

关于javascript - 学习在 Javascript 中使用命名空间和模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23387198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com