简体   繁体   English

如何将多个文件放入单个命名空间/模块中? (JavaScript的)

[英]How do I put multiple files into a single namespace/module? (javascript)

I've been reading about the module pattern, but everything I read assumes that the entire contents of the module will be in a single file. 我一直在阅读有关模块模式的内容,但我读到的所有内容都假设模块的全部内容都在一个文件中。 I want to have one file per class. 我想每班有一个文件。

I've resorted to doing this at the top of every file: 我已经在每个文件的顶部执行此操作:

if(window.ModuleName === undefined) { window.ModuleName = {}; }

ModuleName.ClassName = function () { ... }

But this allows files to be included without their dependencies, which is also annoying. 但是这允许包含文件而没有它们的依赖性,这也很烦人。 For example, lets say there is ClassA which uses ClassB , and "ClassB.js" is left out of the HTML, then ClassA will throw up errors. 例如,假设有一个使用ClassB ClassA ,并且“ClassB.js”被排除在HTML之外,那么ClassA将抛出错误。 As far as I'm aware Javascript lacks an import statement, so in this case I actually want everything to be in a single file. 据我所知,Javascript缺少import语句,所以在这种情况下我实际上希望所有内容都在一个文件中。

I assume that large javascript projects are broken up into multiple files, so there has to be a way around this. 我假设大型javascript项目被分解为多个文件,因此必须有办法解决这个问题。 How is it generally done? 一般情况如何? Is there some tool that will combine multiple class files into a single module file? 是否有一些工具可以将多个类文件合并到一个模块文件中?

This is a big topic but let me explain as much as I can. 这是一个很大的话题,但让我尽可能多地解释一下。 Javascript requires that you have preloaded anything you intended to use, which is why your module pattern has all the "things" in the same file. Javascript要求您预先加载了您打算使用的任何内容,这就是您的模块模式在同一文件中包含所有“内容”的原因。 But if you plan to separate them in different files then you have to manage it before using. 但是,如果您计划将它们分隔在不同的文件中,那么您必须在使用之前对其进行管理。 I suggest the following approaches 我建议采用以下方法

  1. Concatenate them before serving them in the server. 在服务器中提供它们之前连接它们。 For example in jsp, you can create a servlet that returns contenttype = "text/javascript", inside that servlet you can append all the scripts you need in one dynamically generated script then return it to the client. 例如,在jsp中,您可以创建一个返回contenttype =“text / javascript”的servlet,在该servlet中,您可以在一个动态生成的脚本中附加所需的所有脚本,然后将其返回给客户端。

  2. In your ant or maven builds etc, there are configurations where in you can concatenate them the files you want together. 在您的ant或maven构建等中,有一些配置,您可以将它们连接在一起。 This is a common practice therefore you should find many reference in the internet. 这是一种常见的做法,因此您应该在互联网上找到许多参考。

  3. Lazy-load javascripts. 懒惰的javascripts。 This is my preferred way. 这是我的首选方式。 I use Lazyload javascript library . 我使用Lazyload javascript库 Basically I declare the dependencies of certain codes much like "import" in Java, then before i call any of them i load their dependencies. 基本上我声明某些代码的依赖关系很像Java中的“import”,然后在我调用它们之前我加载它们的依赖项。 This allows for optimized dependency loading without scripts redundancies. 这允许在没有脚本冗余的情况下优化依赖性加载。 The problem is you need to write some fairly complicated scripts to do this. 问题是您需要编写一些相当复杂的脚本来执行此操作。

Hope to help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何跨多个文件跨越javascript命名空间? - How to span javascript namespace across multiple files? 如何使用单个javascript properties.js文件在多个HTML文件中设置变量? - How do I use a single javascript properties.js file to set variables in multiple HTML files? Webpack:如何将多个 javascript 文件捆绑到一个输出文件中? - Webpack: How do I bundle multiple javascript files into a single output file? 将JavaScript文件放在Hexo的哪里? - Where do I put the JavaScript files in Hexo? 如何在多个文件的输出打字稿中优化模块名称空间,使用gulp - How optimize module namespace in output typescript of multiple files, use gulp 将JavaScript命名空间拆分为多个文件 - Splitting a JavaScript namespace into multiple files 如何在 JavaScript 中声明命名空间? - How do I declare a namespace in JavaScript? 如何在 JavaScript 中创建命名空间? - How do I create a namespace in JavaScript? 如何将我的JavaScript类拆分为多个文件,但又使它们属于同一名称空间? - How may I split my JavaScript classes across multiple files and yet have them belong to the same namespace? 如何配置ngResource到命名空间中的放置/发布数据? - How do I configure ngResource to namespace it's put/post data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM