如何在文件中组织 OO js 代码?本地网络应用

发布于 2024-08-10 11:43:17 字数 789 浏览 3 评论 0原文

我目前正在开发一个将在B2B 环境中的本地网络上运行的应用程序。所以我几乎可以忘记在节省带宽方面的微型(迷你?)优化,因为硬件便宜,程序员昂贵

我们的项目中有一个结构良好的面向对象的 js 代码,显然还有很多 js 类。如果所有类都存储在单独的文件中,那么浏览此代码并维护它将非常容易。

但这将使我的浏览器生成几十个 HTTP 请求,以获取页面上我需要的所有 js 文件/类。即使在本地环境中,第一次加载(缓存为空)时也不是超级快,稍后当您修改它并且缓存必须失效时。

可能的解决方案:

  • 违反规则“每个文件一个类”
  • 使用YUI 压缩器 始终(在开发和生产中)用于生成一个大的 js 文件。

但是如果我们为此选择 YUI 压缩器(在开发环境中没有缩小操作,而在生产环境中则缩小) - 那么我们需要在任何 js 文件中的每次修改时重新加载/重新编译这个大 js 文件。

您对解决此问题有何建议?

I'm currently developing an application that will be run on local network in B2B environment. So I can almost forget about micro(mini?) optimizations in terms of saving bandwidth because Hardware Is Cheap, Programmers Are Expensive.

We have a well structured Object oriented js code in the project and obviously lots of js classes. If all the classes will be stored in separated files then it will be quite easy to navigate through this code and hence maintain it.

But this will bring my browser to generate a couple dozens of HTTP requests to get all the js files/classes I need on the page. Even in local environment it is not super fast on first load(with empty cache), and later when you modify it and cache has to be invalidated.

Possible solutions:

  • violate rule "one class per file"
  • use YUI compressor all the time(in development & production) for generating one big js file.

But if we choose YUI compressor for this(no minify action in dev environment, and minify for production) - then we need to reload/recompile this big js file on every modification in any js file.

What would you recommend for solving this problem?

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

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

发布评论

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

评论(3

糖粟与秋泊 2024-08-17 11:43:17

将所有 .js 文件分开。保持“每个文件一个类”规则。

然后,使用服务器端技术将脚本聚合到一个请求中。

选项:

  1. 使用 ASPX 或 PHP 或任何您拥有的服务器端脚本,将所有 JS 聚合到一个请求中。对 .js 的请求不再是静态文件,但通过服务器上的缓存,其服务成本应该相对较低。

  2. 在合并的 .js 文件中使用服务器端包含。


Keep all the .js files separate. Keep your "one class per file" rule.

Then, use a server-side technology to aggregate the script into one request.

Options:

  1. Use an ASPX or PHP or whatevver server-side scripting thing you have, to aggregate all the JS into one request. The request for a .js is no longer a static file, but with caching on the server it should be relatively cheap to serve.

  2. Use Server Side Includes in a consolidated .js file.

    <!--#include virtual="/class1.js"-->
    <!--#include virtual="/class2.js"-->

安静被遗忘 2024-08-17 11:43:17

为每个类创建单独的文件的方法是好的 - 使开发更容易的做法总是好的。

以下是一些加快加载速度的技巧:

  • 压缩代码。正如你所说,你可以使用 YUICompressor 或新发布的 Google Closure Compiler
  • 将多个文件连接成一个文件时,请考虑您需要什么以及何时需要:如果您在应用程序启动时只需要文件 A、B 和 C,而不需要 Z 和 X,则只需将 A、B 和 C 放入一个文件中。在 A/B/C 之后同时加载另一个带有 Z 和 X 的文件。
  • 您可以使用 Firefox 插件 YSlowPage Speed 用于测试负载性能瓶颈

正如您提到的,每次进行更改时都需要重新运行压缩器。我不认为这是一个大问题 - 在一台像样的机器上,即使有很多文件,它也应该运行得相当快。或者,您可以使用某种工具进行日常构建过程,该工具可以从源代码管理构建最新版本(您确实使用 scm,对吧?),并运行单元测试并在一切正常时进行部署。

我建议使用 Ant 或其他一些自动化工具来创建构建脚本。这将使它变得像运行一个命令来构建压缩脚本一样简单,从而减少了您原本需要做的重复工作。您甚至可以让 Ant 将代码部署到服务器。

Your approach of having separate files for each class is good - practices that make development easier are always good.

Here's some tips for making the loading faster:

  • Compress your code. As you say, you could use YUICompressor, or the newly released Google Closure Compiler.
  • When concatenating multiple files into one, think of what you need and when: If you only need files A, B and C when the app starts, but not Z and X, put only A, B and C into a single file. Load another file with Z and X concurrently after A/B/C.
  • You can use Firefox plugins YSlow and Page Speed to test for load performance bottlenecks

As you mention, you would need to rerun the compressor each time you make a change. I don't think this is a big problem - on a decent machine, it should run pretty fast even with a lot of files. Alternatively, you could use a daily build process using some tool, which could build the latest revision from your source control (you do use scm, right?), and run unit tests and deploy if everything goes OK.

I would recommend using Ant or some other automation tool to create a build script. This will make it as simple as running one command to build your compressed script, reducing the repetitive work you would otherwise need to do. You could even have Ant deploy your code to the server.

你的笑 2024-08-17 11:43:17

您可能拥有两全其美的优势 - 每个 js 文件一个类的开发环境,无需为每次迭代进行编译/部署,并且在生产中具有一个(或多个)串联的较大 js 文件(如果需要,可以缩小)。

根据您的构建环境,可以通过多种不同的方式进行设置,但使用 Ant 可能是最简单的方法。使用 Ant,您可以运行串联 和缩小任务(通过Java 任务)。这将生成串联且缩小的大型 js 文件。

然而,为了保持生产力,您希望避免在每次代码迭代时都这样做。将标签从一个更改为多个(对于每个类文件)是不可能的。

因此,您可以按预期加载大 js 文件:

<script src="application.js"></script>

部署到生产环境时,此文件是所有 js 文件的串联/缩小版本。

然而,在开发过程中,这个文件是一个引导/加载程序文件,它只是加载所有单独的 js 文件(使用 jQuery 的说明性示例)。

$.getScript('/class1.js');
$.getScript('/class2.js');
$.getScript('/class3.js');
$.getScript('/class4.js');
$.getScript('/classn.js');
....

如果您使用 YUI 3,请查看模块行为以及如何指定依赖项。

使用不同的 Ant 目标,可以轻松管理这些文件的生成和复制到正确的位置。

现在,每当您需要测试文件中的更改时,您只需重新加载浏览器,即可在生产过程中获得性能优势。所有这些都不会牺牲生产力或可维护性。

You may have the best of both worlds - a development environment with one class per js file without the need to compile/deploy for every iteration AND one (or several) concatenated larger js files (minified if desired) in production.

Depending on your build environment this may be setup in a number of different ways, but using Ant may be the easiest way. Using Ant you can run tasks for both concatenation and minification (running YUICompressor through the Java task). This will produce the concatenated and minified large js file.

However, to maintain productivity you want to avoid doing this for every code iteration. Changing the tags from one to several (for every class file) is out of the question.

So, you load your big js file as expected:

<script src="application.js"></script>

When deploying to production this file is the concatenated/minified version of all your js files.

However, during development this file is a bootstrap/loader file that simply loads all your individual js-files (illustrative example using jQuery).

$.getScript('/class1.js');
$.getScript('/class2.js');
$.getScript('/class3.js');
$.getScript('/class4.js');
$.getScript('/classn.js');
....

If you are using YUI 3 look into the module behavior and how to specify dependencies.

Using different Ant targets the generation and copying of these files to the correct location may easily be managed.

And now you may simply reload your browser whenever you need to test a change in a file, but get the performance benefit during production. All without sacrificing productivity or maintainability.

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