将模块模式用于较大的项目 [英] Using the Module Pattern for larger projects
问题描述
我对使用模块模式更好地组织我的未来项目感兴趣.不幸的是,只有很少的简短教程和有关模块模式的概念验证示例.
使用模块模式,我想将项目组织成这种结构:
project.arm.object.method();
其中项目"是我的全局项目名称,臂"是项目的子节或分支,对象"是单个对象,依此类推.
但是,我不确定如何在项目"下声明和组织多个武器"和对象".
var project = window.project || {};
project.arm = project.arm || {};
project.arm.object = (function() {
var privateVar = "Private contents.";
function privateMethod() {
alert(privateVar);
}
return {
method: privateMethod
};
}());
定义复杂的模块结构时是否有最佳实践或约定?我应该在最后一个下面声明一个新的手臂/对象吗?
以下是您所追求的很好的记录; http://www.adequatelygood.com/2010/3/JavaScript -Module-Pattern-Depth
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?
Here's a good write-up on what you're after; http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
这篇关于将模块模式用于较大的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!