有没有组织 Javascript 代码的最佳方法?

发布于 2024-11-01 23:28:59 字数 683 浏览 4 评论 0原文

我一直在做一个小型 Web 应用程序,使用 jQuery 在 Javascript 中添加行为和附加 DOM 对象。问题是代码开始增长得非常快,我不知道将其存储在哪里(要么在正在使用的同一页面中,要么在一个大的 js 文件中)。 此时,我有:

  • 1 个 js 文件,其中包含一个存储一些内容的对象 同步信息来自 服务器,以及最重要的 方法(应该跨使用 申请)。其中之一 方法包含 DOM 元素 生成器(然后我添加 在所需的网页内)。
  • 1 js 文件有一个大方法,它是 当 window.ready 添加时调用 对一堆 DOM 对象的行为(仅与网页相关)。
  • 一些标签已填充 在没有的页面中添加代码 使用第二个js文件。此代码也与网页相关。

现在,你可能会用你的眼睛杀死我,我知道。这绝对不是一个好的(或者至少不一致)的 Javascript 存储方式。所以,我的问题是:

是否有组织 Javascript 代码的最佳方法?

我认为至少我应该遵循一条规则,尽管问题是我不确定是否最好有一个规则将所有“全局数据和方法”保存在 .js 文件中,然后将其他文件存储在较小的 js 文件中或 ,或者干脆放弃并去喝茶。

你们觉得怎么样?提前致谢!

PS:显然代码重用是一个好点。然而,我最常重用的脚本保存在第一个 js 文件中(包含全局内容的文件)。

I've been doing a small web application, adding behaviors and appending DOM objects in Javascript using jQuery. The problem is that the code is starting to grow really fast and I don't know where to store it (either in the same page it's being used, or in a big js file).
At this point, I have:

  • 1 js file with an object storing some
    synchronized information from a
    server, and with the most important
    methods (which should be used across
    the application). One of these
    methods include a DOM element
    generator (which then I add from
    within the desired web page).
  • 1 js
    file with a big method which is
    called when window.ready that adds
    behaviors to a bunch of DOM object (only related to a webpage).
  • Some tags filled
    with code in the pages that didn't
    use the second js file. This code is also webpage related.

Now, you may be killing me with your eyes, and I know. This is definitely not a good (or at least not consistent) way to store Javascript. So, my question is:

Is there a best way to organize Javascript code?

I suppose that at least I should follow a rule, though the problem is that I'm not sure if it's best to have a js file for all the "global data and methods" and then store the other ones in smallers js files or , or simply give up and go for a tea.

What do you guys think? Thanks in advance!

PS: Obviously code reutilization is a good point. However, the scripts I will be reusing the most are saved in the first js file (the one with global stuff).

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

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

发布评论

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

评论(4

习惯成性 2024-11-08 23:28:59

我通常将全局脚本存储在一个公共文件中,每个页面都有其单独的脚本文件。

您可以将页面特定的脚本文件与页面一起存储(在同一文件夹中),或者在公共“脚本”文件夹中复制站点的目录结构(如果您有很多脚本文件,这很有用),这两种方式我都有脚本文件命名为它们的页面对应项,以使映射变得容易。

此外,您还可以使用命名空间来进一步组织代码。

I usually store global scripts in a single common file, and each page has its separate script file.

You can store page specific script files along with the page (in the same folder) or replicate the directory structure of the site inside a common "scripts" folder (useful if you have many many script files), both ways I have the script files named as their page counterpart, to make mappings easy.

Also, you can use namespaces to further organize your code.

凡尘雨 2024-11-08 23:28:59

对于这个问题没有硬性的答案,但要记住的一件事是,如果您在外部文件中放置尽可能多的 javascript,则可以对其进行缓存,这将使后续访问该页面的速度更快。

我倾向于遵循的规则是:

  • 所有可以在多个地方使用的代码都放在外部文件中
  • 外部文件不包含实际执行任何操作的代码,这些文件充当我可以使用的代码库。
  • 该页面本身包含最少的 JavaScript 代码来初始化我的库中的代码。
  • 如果此初始化代码需要超过 10 行左右的代码,那么值得考虑将其放入库中的函数/方法中。
  • 所有 javascript 都尽可能靠近页面底部,就在 标签。这是因为当浏览器遇到
  • 由于上述原因,只有绝对必须放在头部的脚本才会被放在那里。

编辑:如果您在页面中包含大量 javascript 文件,那么值得考虑将它们合并到一个文件中。这是因为页面中的每个

yslow 是一个确定优化以加速页面加载的好工具,它有很多提供有关 javascript 文件优化的建议。 。

There's no hard and fast answer to this one, but one thing to bear in mind is that if you put as much javascript as possible in external files, it can be cached which will make subsequent visits to the page faster.

The rules I tend to follow are:

  • All code that can be used in more than one place goes in external files
  • No external files contain code that actually does anything, the files serve as libraries of code I can use.
  • The page itself contains minimal javascript code to initialize the code in my libraries.
  • If this initialization code takes more than 10 or so lines of code then it's worth looking into putting it into a function/method in the library
  • All javascript goes as near the bottom of the page as possible, just before the </body> tag. This is because when the browser encounters a <script> tag it will stop rendering the page and evaluate the script before continuing. This can make the page feel slower to the user (though there's no actual difference in load time, perception matters!)
  • Only scripts that absolutely must go in the head are put there for the reason described above.

EDIT: If you are including a lot of javascript files in a page then it is worth looking into consolodating them into a single file. This is because every <script src="..."> tag in a page will trigger a HTTP request, and the specs say that there can only ever be two requests open per domain at any one time. Putting them all in a single file will also increase the benefit you will get from using a JS compressor such as closure or YUI, and the benefits from using mod_gzip

yslow is a good tool for determining optimizations to speed up page loading, it has plenty of advice to offer regarding javascript file optimizations. .

彡翼 2024-11-08 23:28:59

如果您使用面向对象的设计策略,那么“代码在哪个文件中”的答案几乎不言而喻。

javascript 的重要部分(类定义和大量方法)应该放入适当的 .js 文件中,(类定义通常有自己的 .js 文件)
但初始化代码应该进入使用它们的 html 页面。

jQuery 提供了一个很好的例子:

它有自己的 .js 文件用于主类和函数定义,
它允许您添加执行特定任务的插件,每个插件都在自己的 .js 文件中。

一旦您想使用 jQuery,只需添加 jQuery 脚本即可。
也想要这个页面上的插件吗?也添加那个。

If you use an object oriented design strategy, the answer to 'in which file does the code go' almost answers itself.

The beefy parts of the javascript (your class definitions and massive methods) should go into appropriate .js files, (a class definition usually gets its own .js file)
but the initialization code should go into the html page where they are utilized.

jQuery offers a good example :

it has its own .js file for the main class and function definitions,
and it allows you to add plugins which perform specific tasks, each in their own .js file.

Once you want to use jQuery, just add the jQuery script.
Want the plugin on this page too? add that one as well.

゛清羽墨安 2024-11-08 23:28:59

我想我理解你的问题是正确的,所以你可以用模块组织你的代码,这些模块将进一步连接在一个文件中。

对于模块,您可以使用 CommonJS 技术。它看起来像这样:

(function(window) {
  var Module = {};

  window.Module = Module;
})(window);

另外:

(function(module) {
  module.NewProp = {};

})(window.Module);

当然,您可以使用自己的变量来代替窗口等。

而且,如果您将使用 Node.js - 这是在服务器和客户端之间共享变量的好方法。

(function(exports) {
  exports.Func = function(){};

})(typeof exports === 'undefined' ? wow : exports);

希望有帮助!

I guess I understand your question right, so you can organize your code with modules, which will be connected in one file further.

For modules you can use CommonJS technique. It looks like this:

(function(window) {
  var Module = {};

  window.Module = Module;
})(window);

And addition:

(function(module) {
  module.NewProp = {};

})(window.Module);

Of course, you can use your own variables instead window etc.

And, if you will use Node.js - it's a good way to share variables between server and client.

(function(exports) {
  exports.Func = function(){};

})(typeof exports === 'undefined' ? wow : exports);

Hope it helps!

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