Javascript 模块化大对象
我有一个定义为函数的对象,其中有很多方法和属性,有哪些方法可以对其进行模块化以使其更易于管理?是否可以将对象内部的函数放入外部文件中?
编辑:
有人在一个答案中提到包含其他文件,但这会产生一个笨拙的 HTML 页面,IE:
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
....
<script type="text/javascript" src="script76.js"></script>
是否还有任何方法来构造代码,以便只需要一个 javascript 文件的引用?
I've got an object defined as a function, and it's got a lot of methods and properties in it, what are some ways to modularise this to make it more manageable? It is possible to put functions inside the object into external files?
Edit:
Someone mentioned in one answer to include other files, but this would have an unweildy HTML page, IE:
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
....
<script type="text/javascript" src="script76.js"></script>
Is there also any way to structure the code so only one reference is required the the javascript file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
并在上述代码之后包含的另一个js文件中
and in another js file included after the above code
您可能需要查看 RequireJS。它允许您定义函数、模块、依赖项并从 Javascript 加载外部 Javascript 文件(可能是异步的)。
You may want to check out RequireJS. It lets you define functions, modules, dependencies and load external Javascript files from Javascript (possibly async).
您可以使用服务器端脚本来处理您的 JavaScript,使用 jsmin 您甚至可以加快该过程(如果缓存)。
我正在考虑这样的脚本:
script.php
您也可以将所有 js 文件包含在一个文件夹中,只需通过文件夹扫描或其他方式生成数组,我想您明白了!
问候内克苏姆
you could use a server side script to handle your javascripts, with jsmin you could even speed up the process (if cached).
i'm thinking of a script somthing like this:
scripts.php
you could also just include all js files inside a folder, just generate the array via folder scanning or something, i think you get the idea!
greetings Nexum
你可以试试这个:
http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm
...但最佳实践是仅在 html 主页面中包含您想要的文件
You could try this:
http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm
...but best practices are to just include the files you want in the main html page