Jam*_*e C 15 javascript design-patterns namespaces
我想为我的应用程序创建一个全局命名空间,在该命名空间中我想要其他命名空间:
例如
Dashboard.Ajax.Post()
Dashboard.RetrieveContent.RefreshSalespersonPerformanceContent();
Run Code Online (Sandbox Code Playgroud)
我还想将它们放在单独的文件中:
但是我尝试使用这种方法,但它不起作用,因为在2个单独的位置使用相同的变量名称作为命名空间.有人可以提供另类选择吗?
谢谢.
如果已经创建了命名空间对象,您只需要确保不要踩踏命名空间对象.像这样的东西会起作用:
(function() {
// private vars can go in here
Dashboard = Dashboard || {};
Dashboard.Ajax = {
Post: function() {
...
}
};
})();
Run Code Online (Sandbox Code Playgroud)
并且RetrieveContent文件将以类似方式定义.
这是一篇关于JavaScript中各种"模块模式"的非常好的文章.关于如何扩充模块或命名空间以及维护跨文件私有状态,有一个非常好的小部分.也就是说,单独文件中的代码将按顺序执行,并在执行后正确扩充名称空间.
我没有彻底探索这种技术,所以没有承诺......但这是基本的想法.
dashboard.js
(function(window){
var dashboard = (function () {
var my = {},
privateVariable = 1;
function privateMethod() {
// ...
}
my.moduleProperty = 1;
my.moduleMethod = function () {
// ...
};
return my;
}());
window.Dashboard = dashboard;
})(window);
Run Code Online (Sandbox Code Playgroud)
dashboard.ajax.js
var dashboard = (function (my) {
var _private = my._private = my._private || {},
_seal = my._seal = my._seal || function () {
delete my._private;
delete my._seal;
delete my._unseal;
},
_unseal = my._unseal = my._unseal || function () {
my._private = _private;
my._seal = _seal;
my._unseal = _unseal;
};
// permanent access to _private, _seal, and _unseal
my.ajax = function(){
// ...
}
return my;
}(dashboard || {}));
Run Code Online (Sandbox Code Playgroud)
dashboard.retrieveContent.js
var dashboard = (function (my) {
var _private = my._private = my._private || {},
_seal = my._seal = my._seal || function () {
delete my._private;
delete my._seal;
delete my._unseal;
},
_unseal = my._unseal = my._unseal || function () {
my._private = _private;
my._seal = _seal;
my._unseal = _unseal;
};
// permanent access to _private, _seal, and _unseal
my.retrieveContent = function(){
// ...
}
return my;
}(dashboard || {}));
Run Code Online (Sandbox Code Playgroud)
你可以做这样的事情......
使用命名空间库的 HTML 页面:
<html>
<head>
<title>javascript namespacing</title>
<script src="dashboard.js" type="text/javascript"></script>
<script src="ajax.js" type="text/javascript"></script>
<script src="retrieve_content.js" type="text/javascript"></script>
<script type="text/javascript">
alert(Dashboard.Ajax.Post());
alert(Dashboard.RetrieveContent.RefreshSalespersonPerformanceContent());
Dashboard.RetrieveContent.Settings.Timeout = 1500;
alert(Dashboard.RetrieveContent.Settings.Timeout);
</script>
</head>
<body>
whatever...
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
仪表板.js:
(function(window, undefined){
var dashboard = {};
window.Dashboard = dashboard;
})(window);
Run Code Online (Sandbox Code Playgroud)
Ajax.js:
(function(){
var ajax = {};
ajax.Post = function() { return "Posted!" };
window.Dashboard.Ajax = ajax
})();
Run Code Online (Sandbox Code Playgroud)
Retrieve_Content.js:
(function(){
var retrieveContent = {};
retrieveContent.RefreshSalespersonPerformanceContent = function() {
return "content retrieved"
};
var _contentType;
var _timeout;
retrieveContent.Settings = {
"ContentType": function(contentType) { _contentType = contentType; },
"ContentType": function() { return _contentType; },
"Timeout": function(timeout) { _timeout = timeout; },
"Timeout": function() { return _timeout; }
};
window.Dashboard.RetrieveContent = retrieveContent;
})();
Run Code Online (Sandbox Code Playgroud)
Dashboard.js 充当其下所有命名空间的起点。其余的在各自的文件中定义。在 Retrieve_Content.js 中,我在下面添加了一些额外的属性,Settings以便在需要时了解如何执行此操作。