[英]What is the best way to deal with this Namespace JavaScript Structure?
i have an object as namespace with three main-objects. 我有一个对象作为带有三个主要对象的名称空间。 I call this object one time and after this it is working for itself. 我称这个对象一次,此后它本身就起作用了。
My Code is structured like this: 我的代码的结构如下:
var Application = Application || {};
Application.Module1 = {
//Code with some functions
}
Application.Module2 = {
//Code with some functions
}
Application.Module3 = {
//Code with some functions
}
(function(){
Application.Module1.Start();
})();
Primarily the modules working among themselves. 主要是模块之间相互工作。 I would like to call for example when i'm in Module1 a function in Module2 like: 例如,当我在Module1中时,我想调用一个类似Module2中的函数:
Module2.randomFunction();
and not: 并不是:
Application.Module2.randomFunction();
But i think it is a bad idea to go 但是我认为这是一个坏主意
Application.Module1.Start.call(Application)
because i'm using also other objects like jQuery. 因为我也在使用其他对象,例如jQuery。
What do you think would be the right way? 您认为正确的方法是什么?
You can use a pattern like this - 您可以使用这样的模式-
var Application = (function() {
var Module1 = {
function1 : function() {
console.log("function1");
Module2.function2();
}
};
var Module2 = {
function2 : function() {
console.log("function2");
}
};
var Module3 = {
function3 : function() {
console.log("function3");
}
};
return {
Module1: Module1,
Module2: Module2,
Module3: Module3
}
})();
Application.Module1.function1();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.