将模块模式用于大型项目

发布于 2024-08-30 01:05:51 字数 610 浏览 7 评论 0原文

我有兴趣使用模块模式来更好地组织我未来的项目。不幸的是,只有一些模块模式的简短教程和概念验证示例。

使用模块模式,我想将项目组织成这种结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街道布景 2024-09-06 01:05:51

这是一篇关于您所追求的内容的好文章; http://www.adequatelygood.com/2010/3/JavaScript -模块模式深入

誰認得朕 2024-09-06 01:05:51

Dojo 的 dojo.declare 非常适合这种情况自从那件事以来

使用继承和原型扩展的紧凑表示法创建构造函数。

即使只是删除这种样板,它也非常方便:

var project = window.project || {};
project.arm = project.arm || {};

如果您只是想要该功能,那么您可以使用 dojo.setObject,但是当然,编写一些东西来做同样的事情是微不足道的。

dojo.setObject("project.arm.object" (function() {
    var privateVar = "Private contents.";

    function privateMethod() {
        alert(privateVar);
    }

    return {
        method: privateMethod
    };
}()));

我最近在一个大型 JavaScript 项目(86 个文件,7K+ 行(不包括注释和空行))中使用了 dojo.declare/dojo.setObject,这非常简单保持一切井然有序且易于管理,尤其是当您拥有像 dojo 这样的包含机制时。需要dojo.provide

Dojo's dojo.declare is great for this kind of thing since it

Create a constructor using a compact notation for inheritance and prototype extension.

It's also really convenient if even for just removing this kind of boiler plate:

var project = window.project || {};
project.arm = project.arm || {};

If you just want that feature, then you could use dojo.setObject, but of course, writing something to do the same is trivial.

dojo.setObject("project.arm.object" (function() {
    var privateVar = "Private contents.";

    function privateMethod() {
        alert(privateVar);
    }

    return {
        method: privateMethod
    };
}()));

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 like dojo.require and dojo.provide.

叹倦 2024-09-06 01:05:51

人们喜欢如何做到这一点有很多细微差别,但是您所称的模块模式(命名范围)的主要好处是,您不会弄乱全局命名空间,这有助于保持事物的整洁,如果您引入其他库等,并避免名称冲突。

如何组织其中的名称和嵌套范围很大程度上取决于个人喜好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文