有没有组织 Javascript 代码的最佳方法?
我一直在做一个小型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常将全局脚本存储在一个公共文件中,每个页面都有其单独的脚本文件。
您可以将页面特定的脚本文件与页面一起存储(在同一文件夹中),或者在公共“脚本”文件夹中复制站点的目录结构(如果您有很多脚本文件,这很有用),这两种方式我都有脚本文件命名为它们的页面对应项,以使映射变得容易。
此外,您还可以使用命名空间来进一步组织代码。
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.
对于这个问题没有硬性的答案,但要记住的一件事是,如果您在外部文件中放置尽可能多的 javascript,则可以对其进行缓存,这将使后续访问该页面的速度更快。
我倾向于遵循的规则是:
标签。这是因为当浏览器遇到
标记时,它将停止渲染页面并在继续之前评估脚本。这可能会让用户感觉页面变慢(尽管加载时间没有实际差异,但感知很重要!)
编辑:如果您在页面中包含大量 javascript 文件,那么值得考虑将它们合并到一个文件中。这是因为页面中的每个
标记都会触发 HTTP 请求,并且规范规定每个域在任何时候都只能打开两个请求一度。将它们全部放在一个文件中还会增加您从使用 JS 压缩器(例如 Closure 或 YUI)中获得的好处,并且使用 mod_gzip 的好处
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:
</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!)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_gzipyslow is a good tool for determining optimizations to speed up page loading, it has plenty of advice to offer regarding javascript file optimizations. .
如果您使用面向对象的设计策略,那么“代码在哪个文件中”的答案几乎不言而喻。
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.
我想我理解你的问题是正确的,所以你可以用模块组织你的代码,这些模块将进一步连接在一个文件中。
对于模块,您可以使用 CommonJS 技术。它看起来像这样:
另外:
当然,您可以使用自己的变量来代替窗口等。
而且,如果您将使用 Node.js - 这是在服务器和客户端之间共享变量的好方法。
希望有帮助!
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:
And addition:
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.
Hope it helps!