在项目中良好的JavaScript结构? [英] Good structuring of JavaScript in a project?

查看:91
本文介绍了在项目中良好的JavaScript结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我现在已经在我的网站上完成了JS。它涉及jQuery(以及这样的.ready init)
该页面有两部分,上部主要是谷歌地图,下部是输入表格。

So, I'm fairly done with the JS in my website now. It involves jQuery (and as such a .ready init) The page has two parts, the upper is mainly Google Maps and the lower is input forms.

所有这些目前都在 one .js,函数,inits,迭代,所有这些。
结构很好,一切都做得不错。

All of this is currently in one .js, functions, inits, iterations, all of it. It's well structure and all that, everything is properly done.

但我的问题是:什么是好的结构?
我应该将上半部分放在一个文件中而将下半部分放在另一个文件中吗?
或者我应该在.ready()下放置所有需要的初始化并将所有函数放在另一个文件中?
或者我应该将所有内容保存在不断增长的文件中吗?

My question is however: What is a good structure? Should I be putting the upper half in one file and the lower in another? Or should I put all the needed initializations under the .ready() and place all functions in another file? Or should I keep everything in an ever growing file?

推荐答案

从性能角度来看,提供所有JS根据其他答案的建议将单个文件发送到浏览器是明智的。但是,以这种方式构建代码不是,每个类应该在一个单独的文件中,将事物分成实体和一个或两个控制文件来处理实际的页面调用 - 与任何其他语言相同。然后可以将这些文件合并到一个文件中,以便提前或动态地提供 - 最好也可以最小化。

From a performance perspective, supplying all your JS in a single file to the browser as suggested by the other answers is sensible. However, having your code built that way is not, each "class" should be in a separate file, splitting things up in to entities and a control file or two to handle the actual page calls - the same as in any other language. These can then be combined in to one file for supply either in advance, or dynamically - preferably also minimized.

这篇关于在项目中良好的JavaScript结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆