不同文件之间的提升

发布于 2024-10-01 12:59:14 字数 337 浏览 1 评论 0原文

有没有办法在不同文件中的源代码之间进行函数和变量提升?也就是说,类似的事情

//Inside firstfile.js
foo === "bar" //should return true

//Inside secondfile.js
function bar() {
    this.foo = "bar";
}

想这是不可能的,因为大多数 javascript 引擎会按顺序单独解析和执行不同的文件,但我不确定。

我不知道这是否在 ECMA 的规范中,因为不同文件的解析实际上并不是该语言的一部分。

Is there a way to do functions and variable hoisting between source code present in different files? That is, something like

//Inside firstfile.js
foo === "bar" //should return true

and

//Inside secondfile.js
function bar() {
    this.foo = "bar";
}

I guess this is not possible, as different files as parsed and executed separately and in order by most javascript engines, but I do not know for sure.

I don't know even if this is in the spec from ECMA, as the parsing of different files is not really part of the language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼痛 2024-10-08 12:59:14

我最初认为这是一个比实际出现的更有趣的问题。我认为这个问题的答案是每个

例如,在单个脚本中:

<script type="text/javasscript">
    alert(typeof foo);
    function foo() {}
</script>

... 警告“function”,因为函数声明已提升,但将两行拆分为单独的

<script type="text/javasscript">
    alert(typeof foo);
</script>
<script type="text/javasscript">
    function foo() {}
</script>

... 警告“undefined” ”,因为第一个脚本是在第二个脚本被解析之前执行的。

I originally thought this was a more interesting question than it actually appears. The answer to the question I thought this was is that each <script> element represents a separate Program, each separate Program is parsed and (crucially) executed before anything happens to the next Program, therefore the effects of hoisting can only be seen within a single Program.

For example, within a single script:

<script type="text/javasscript">
    alert(typeof foo);
    function foo() {}
</script>

... alerts "function" because the function declaration is hoisted, but splitting the two lines into separate <script> elements changes this:

<script type="text/javasscript">
    alert(typeof foo);
</script>
<script type="text/javasscript">
    function foo() {}
</script>

... alerts "undefined", because the first script is executed before the second script is even parsed.

风吹过旳痕迹 2024-10-08 12:59:14

单独加载的 Javascript 源都会影响全局上下文。因此,

window.foo = "bar";

在一个源文件中将允许另一个源(随后加载)进行检查:

if (window.foo === "bar") {
  // do something
}

这必须有效,否则将无法创建类似于所有流行的 Javascript 框架的东西。

“this”关键字仅在函数内部有意义,其值与函数所在的源文件无关(至少没有任何直接意义)。

编辑 - 我想这里有趣的是Javascript解释器行为(使用问题中的术语)在正在评估的块中的其他代码之前“提升”函数声明。当浏览器加载它们时,这也是逐个脚本完成的。因此,脚本块中的函数声明在每个块中的其他代码之前被解释,但单个

如果您使用 LabJS 或enhance.js 之类的东西来加载脚本,事情会变得更加复杂,但我不知道在任何上下文中您可以依赖脚本以某种方式“混合在一起”除非您明确在服务器上组合它们。

Separately-loaded Javascript sources can all affect the global context. Thus

window.foo = "bar";

in one source file will allow another source (loaded subsequently) to check:

if (window.foo === "bar") {
  // do something
}

This has to work, or else it would not be possible to create something like all the popular Javascript frameworks.

The "this" keyword is only meaningful inside a function, and its value has nothing to do with the source file from which the function came (at least, nothing in any direct sense).

edit — I guess that the interesting thing here is the Javascript interpreter behavior that (to use the term in the question) "hoists" function declarations up before other code in a block being evaluated. That also is done on a script-by-script basis when the browser is loading them. Thus, function declarations in a script block are interpreted before other code in each block, but a single <script> tag will be completely evaluated before the next <script> tag is loaded and evaluated.

Things get more complicated if you're using something like LabJS or enhance.js to load scripts, but I don't know of any context wherein you could rely on scripts "blending together" somehow unless you explicitly combine them at the server.

夏花。依旧 2024-10-08 12:59:14

浏览器遇到的所有脚本标签都会立即解析并执行。这是因为 document.write API 可能需要浏览器向 DOM 输出一些内容。这意味着在这种情况下#script1 必须在#script2 之前完成。

<script id="script1"></script>
<script id="script2"></script>

您可能想检查使用 ASYNC 和 DEFER 属性时的行为。它们在 Webkit 中得到支持。

All script tags that browser encounters are parsed and executed immediately. This is because of document.write API, that might require to browser to output something to the DOM. This means that in this situation #script1's has to finish before #script2.

<script id="script1"></script>
<script id="script2"></script>

You might want to check what the behaviour when using ASYNC and DEFER attributes. They are supported in Webkit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文