将模块模式用于大型项目
我有兴趣使用模块模式来更好地组织我未来的项目。不幸的是,只有一些模块模式的简短教程和概念验证示例。
使用模块模式,我想将项目组织成这种结构:
project.arm.object.method();
其中“project”是我的全局项目名称,“arm”是项目的子部分或分支,“object”是单个对象,并且等等方法和属性。
但是,我不确定应该如何在“项目”下声明和组织多个“手臂”和“对象”。
var project = window.project || {};
project.arm = project.arm || {};
project.arm.object = (function() {
var privateVar = "Private contents.";
function privateMethod() {
alert(privateVar);
}
return {
method: privateMethod
};
}());
定义复杂的模块结构时是否有任何最佳实践或约定?我应该在最后一个手臂/物体下面声明一个新的手臂/物体吗?
I'm interested in using the Module Pattern to better organize my future projects. Unfortunately, there are only a few brief tutorials and proof-of-concept examples of the Module Pattern.
Using the module pattern, I would like to organize projects into this sort of structure:
project.arm.object.method();
Where "project" is my global project name, "arm" is a sub-section or branch of the project, "object" is an individual object, and so on to the methods and properties.
However, I'm not sure how I should be declaring and organizing multiple "arms" and "objects" under "project".
var project = window.project || {};
project.arm = project.arm || {};
project.arm.object = (function() {
var privateVar = "Private contents.";
function privateMethod() {
alert(privateVar);
}
return {
method: privateMethod
};
}());
Are there any best practices or conventions when defining a complex module structure? Should I just declare a new arm/object underneath the last?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一篇关于您所追求的内容的好文章; http://www.adequatelygood.com/2010/3/JavaScript -模块模式深入
Here's a good write-up on what you're after; http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
Dojo 的
dojo.declare
非常适合这种情况自从那件事以来即使只是删除这种样板,它也非常方便:
如果您只是想要该功能,那么您可以使用
dojo.setObject
,但是当然,编写一些东西来做同样的事情是微不足道的。我最近在一个大型 JavaScript 项目(86 个文件,7K+ 行(不包括注释和空行))中使用了
dojo.declare
/dojo.setObject
,这非常简单保持一切井然有序且易于管理,尤其是当您拥有像dojo 这样的包含机制时。需要
和dojo.provide
。Dojo's
dojo.declare
is great for this kind of thing since itIt's also really convenient if even for just removing this kind of boiler plate:
If you just want that feature, then you could use
dojo.setObject
, but of course, writing something to do the same is trivial.I recently used
dojo.declare
/dojo.setObject
for a large JavaScript project (86 files, 7K+ lines (not counting comments and blank lines)), and it was a breeze to keep everything organized and manageable, especially when you have an inclusion mechanism likedojo.require
anddojo.provide
.人们喜欢如何做到这一点有很多细微差别,但是您所称的模块模式(命名范围)的主要好处是,您不会弄乱全局命名空间,这有助于保持事物的整洁,如果您引入其他库等,并避免名称冲突。
如何组织其中的名称和嵌套范围很大程度上取决于个人喜好。
There are a lot of nuances to how people prefer to do that, but the main benefit of what you're calling the module pattern (a named scope), is that your not cluttering up the global namespace, which helps keep things clean if you bring in other libraries etc, and avoids name collisions.
How you organize the names and nested scopes within that is largely a matter of personal preference.