requireJS:如何为整个网站构建 Javascript 结构?

发布于 2024-10-19 17:02:16 字数 372 浏览 4 评论 0原文

我有 3000 多行 javascript,我需要将它们变成一个合理/可维护的结构。我选择使用 requireJS 因为它已经被一些人推荐给我了。我有一堆在整个应用程序中使用的变量,并且需要在任何地方都可用。我还有一堆需要随处可用的功能。除了这两个依赖项之外,大多数代码都可以分为自己的模块。

我无法理解如何管理我的主要变量,这样如果一个代码模块对变量进行更改,其余的 JS 模块就会看到该变化。我想我需要看一些示例演示了 requireJS 如何在比文档中的示例更大规模的情况下工作。

如果有人是经验丰富的 requireJS 用户,我很乐意听到您的提示!

I have 3000+ lines of javascript that I need to get into a sensible/maintainable structure. I have chosen to use requireJS as it has been recommend to me by a few people. I have a bunch of variables that are used throughout the application and need to be available everywhere. I also have a bunch of functions that need to be available everywhere. Apart from these two dependencies most of the code can be divided off into their own modules.

I am having trouble understanding how to manage my main variables so that if one module of code makes changes to the variables the rest of the JS modules will see that change. I think I need to see a few examples that demonstrate how requireJS is intended to work on a larger scale that the examples in the documentation.

If anyone is an experienced requireJS user I would love the hear your tips!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

给不了的爱 2024-10-26 17:02:17

RequireJS 的重点是避免对这些全局变量和全局函数的需要。

您不能将这些全局变量包装到一个模块中,然后在其他模块中依赖它吗?

例如,一个 RequireJS 模块化 Dojo 可能类似于:

dojo/cache module
dojo/string module (requires dojo/cache)
dojo/date module (requires dojo/string)
dojo/cookie module (requires dojo/string)
           :
           :
dojo module (requires everything above, make them all into sub-objects, say, e.g. dojo.cache, dojo.string, dojo.date etc.)

user module #1 (requires dojo)
user module #2 (maybe only requiring dojo/string)

The whole point of RequireJS is to avoid the need for these global variables and global functions.

Can't you wrap those globals into a module, then depend on it in your other modules?

For example, a RequireJS modularized Dojo may be something like:

dojo/cache module
dojo/string module (requires dojo/cache)
dojo/date module (requires dojo/string)
dojo/cookie module (requires dojo/string)
           :
           :
dojo module (requires everything above, make them all into sub-objects, say, e.g. dojo.cache, dojo.string, dojo.date etc.)

user module #1 (requires dojo)
user module #2 (maybe only requiring dojo/string)
陪我终i 2024-10-26 17:02:17

RequireJS 为您提供了更好的封装模块选项,但它根本不会改变 Javascript 本身。作为过渡策略,您仍然可以在功能块内定义全局变量。包含这些定义的依赖模块将确保它在依赖模块之前运行。

下一步是将这些方法分配给除 window 之外的对象,然后通过从 RequireJS 模块依赖项接收的变量使用该对象。

希望当您完成此操作时,您可能会对更清洁的解决方案有所了解。我将(现在仍然是)一个单文件项目重构为多个文件,包括可选插件,尽管我在将 RequireJS 添加到混合中之前完成了大部分工作。

RequireJS gives you better options for encapsulating modules, but it doesn't change Javascript itself at all. As a transition strategy, you can still define your globals inside the function block. Depending on the module that contains these definitions will ensure that it has run before the dependent modules.

Next step would be to assign those methods to an object other than window, and then use that object through the variable received from RequireJS module dependency.

Hopefully by the time you've done this, you might have some insight into a cleaner solution. I refactored (and still am) a single-file project into several files, including optional plug-ins, although I did most of it before adding RequireJS to the mix.

暮凉 2024-10-26 17:02:17

请参阅 RequireJS 文档:

如果模块具有依赖项,则第一个参数应该是依赖项名称数组,第二个参数应该是定义函数。 ...依赖项将作为函数参数传递给定义函数

define(["./cart", "./inventory"], function(cart, inventory) {
        // ...
        return { ... };
    }
);

所以我认为您可以像所有其他模块一样定义()您的主模块并使子模块依赖于该主模块。然后模块对象作为参数传递给子模块的定义函数。您不必使用全局变量。

See the RequireJS documentation:

If the module has dependencies, the first argument should be an array of dependency names, and the second argument should be a definition function. ... The dependencies will be passed to the definition function as function arguments

define(["./cart", "./inventory"], function(cart, inventory) {
        // ...
        return { ... };
    }
);

So I think you can define() your main module like all other modules and make the submodules depend on that main module. Then the module object is passed as an argument to the definition function of a submodule. You don't have to use global variables.

仅一夜美梦 2024-10-26 17:02:17

如果要在模块之间共享信息,请将信息附加到模块对象,并让其他模块依赖该模块,并检查其属性。

如果您有现有代码,则可以在清理时分配给 window.x 以提供全局 x

If you want to share information between modules, attach the information to the module object, and have the other modules rely on that module, and check its property.

If you have existing code, you can assign to window.x to provide a global x while you are cleaning it up.

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