问题描述
我想为我的应用程序创建一个全局名称空间,在该名称空间中,我想要其他名称空间:
例如
Dashboard.Ajax.Post() Dashboard.RetrieveContent.RefreshSalespersonPerformanceContent();
我也想将它们放在单独的文件中:
- ajax.js
- dretievecontent.js
但是,我尝试使用 this 方法,但是它行不通,但是因为在2个单独的位置中的名称空间使用相同的变量名称.谁能提供替代方案?
谢谢.
推荐答案
,如果已经创建了命名空间对象,则只需要确保不要踩在命名空间对象上即可.这样的事情会起作用:
(function() { // private vars can go in here Dashboard = Dashboard || {}; Dashboard.Ajax = { Post: function() { ... } }; })();
和RetrieveContent文件的定义类似.
其他推荐答案
我没有彻底探索这种技术,所以没有承诺...但是这是基本思想.
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);
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 || {}));
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 || {}));
其他推荐答案
yahoo namepace 函数是为此问题而设计的.
添加:
source source 可用.您可以根据需要将其复制到自己的代码中,将根从Yahoo更改为其他东西等.
.问题描述
I want to create a global namespace for my application and in that namespace I want other namespaces:
E.g.
Dashboard.Ajax.Post() Dashboard.RetrieveContent.RefreshSalespersonPerformanceContent();
I also want to place them in seperate files:
- Ajax.js
- RetrieveContent.js
However I have tried using this method, however it won't work because the same variable name is being used for the namespace in 2 seperate places. Can anyone offer an alternative?
Thanks.
推荐答案
You just need to make sure that you don't stomp on your namespace object if it's already been created. Something like this would work:
(function() { // private vars can go in here Dashboard = Dashboard || {}; Dashboard.Ajax = { Post: function() { ... } }; })();
And the RetrieveContent file would be defined similarly.
其他推荐答案
Here is a very good article on various "Module Patterns" in JavaScript. There is a very nice little section on how you can augment modules, or namespaces and maintain a cross-file private state. That is to say, the code in separate files will be executed sequentially and properly augment the namespace after it is executed.
I have not explored this technique thoroughly so no promises... but here is the basic idea.
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);
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 || {}));
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 || {}));
其他推荐答案
The Yahoo Namespace function is exactly designed for this problem.
Added:
The source of the function is available. You can copy it into your own code if you want, change the root from YAHOO to something else, etc.