简体   繁体   English

为什么javascript没有更好的方法来包含文件?

[英]Why doesn't javascript have a better way to include files?

I've seen a few ways that you can make a javascript file include other javascript files, but they all seem pretty hacky - mostly they involve tacking the javascript file onto the end of the current document and then loading it in some way. 我已经看到了一些方法,你可以使javascript文件包含其他javascript文件,但它们都看起来很hacky - 主要是他们涉及将javascript文件添加到当前文档的末尾,然后以某种方式加载它。

Why doesn't javascript just include a simple "load this file and execute the script in it" include directive? 为什么javascript只包含一个简单的“加载此文件并在其中执行脚本”include指令? It's not like this is a new concept. 这不是一个新概念。 I know that everyone is excited about doing everything in HTML5 with javascript etc, but isn't it going to be hard if you have to hack around omission of basic functionality like this? 我知道每个人都对使用javascript等在HTML5中做所有事情感到很兴奋,但是如果你不得不在这些基本功能的遗漏中乱七八糟,那会不会很难?

I can't see how it would be a security concern, since a web page can include as many javascript files as it likes, and they all get executed anyway. 我无法看到它将如何成为一个安全问题,因为一个网页可以包含尽可能多的javascript文件,无论如何它们都会被执行。

The main problems with the current inclusion system (ie, add additional script tags) involve latency. 当前包含系统的主要问题(即,添加额外的脚本标签)涉及延迟。 Since a script tag can insert code at the point of inclusion, as soon as a script tag is encountered, further parsing has to more-or-less stop until the JS downloads and is executed (although the browser can continue to fetch resources in parallel). 由于脚本标记可以在包含时插入代码,因此只要遇到脚本标记,进一步解析就必须或多或少地停止,直到JS下载并执行(尽管浏览器可以继续并行获取资源) )。 If the JS decides to run an inclusion, you've just added more latency on top of this - now you can't even fetch your scripts in parallel. 如果JS决定运行包含,那么你刚刚添加了更多的延迟 - 现在你甚至无法并行获取脚本。

Basically, it's trying to solve a problem that doesn't exist (since JS can already tack on additional script tags to do an inclusion), while making the latency problem worse. 基本上,它正试图解决一个不存在的问题(因为JS已经可以在其他脚本标签上进行包含),同时使延迟问题变得更糟。 There are javascript minifiers out there that can merge JS files; 有javascript minifiers可以合并JS文件; you should look into using those instead, as they will help improve latency issues as well. 你应该考虑使用它们,因为它们也有助于改善延迟问题。

Actually, YUI 3 solves this problem beautifully. 实际上,YUI 3很好地解决了这个问题。 Feel free to check out the documentation: http://developer.yahoo.com/yui/3/yui/#use (that's the specific Use function which does this magic). 请随时查看文档: http//developer.yahoo.com/yui/3/yui/#use (这是具有这种魔力的特定使用功能)。 Basically it works like this: 基本上它的工作原理如下:

  • You define modules 您定义模块
  • When you create the core YUI object with YUI(), you specify which modules your code needs 使用YUI()创建核心YUI对象时,可以指定代码所需的模块
  • Behind the scenes, YUI checks if those modules are loaded. 在幕后,YUI检查是否加载了这些模块。 If not, it asynchronously loads them on the page. 如果没有,它会在页面上异步加载它们。

I've also read that the jQuery team's working on something similar (someone back me up here). 我还读到jQuery团队正在研究类似的东西(有人支持我)。

As to the philosophical argument that it'd be nice if this was built in, I think that may be a good feature. 至于哲学论点,如果内置它会很好,我认为这可能是一个很好的特征。 On the other hand, the simplicity of javascript is nice too. 另一方面,javascript的简单性也很好。 It allows a much lower point of entry for beginning programmers to do their thing. 它允许初级程序员完成他们的工作的低得多的入口点。 And for those of us that need it, great libraries like YUI are getting better every day. 对于我们这些需要它的人来说,像YUI这样的大型图书馆每天都在变得越来越好。

the requirejs project attempts to solve this problem, please see for example requirejs项目尝试解决此问题,请参阅示例

http://requirejs.org/docs/why.html http://requirejs.org/docs/why.html

(I don't use it yet, though) (不过我还没用过)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM