本文介绍了有多少JavaScript程序在浏览器中一个单一的网页执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript程序由语句和函数声明。当执行JavaScript程序,出现以下两个步骤:

JavaScript programs consist of statements and function declarations. When a JavaScript program is executed, these two steps occur:


  1. 在code为扫描函数声明,和每一个FUNC。声明执行(创建一个函数对象)和一个名为引用,该函数创建(使该功能可以从一个语句中调用)

  1. the code is scanned for function declarations, and every func. declaration is "executed" (by creating a function object) and a named reference to that function is created (so that this function can be called from within a statement)

(因为它们出现在code)执行该语句(评估)依次

the statements are executed (evaluated) sequentially (as they appear in the code)

正因为如此,这个作品就好

<script>
    foo();
    function foo() {
        return;
    }
</script>

虽然宣布收到foo的函数被调用,它的工作原理是因为函数声明语句之前评估。

Although the "foo" function is called before it is declared, it works because the function declaration is evaluated before the statement.

然而,这不起作用

<script>
    foo();
</script>
<script>
    function foo() {
        return;
    }
</script>

一个的ReferenceError将被抛出(没有定义富)。
这导致该网页重新$ P $的HTML code内的每个SCRIPT元素psents一个独立的JavaScript程序和每一个HTML解析器遇到SCRIPT元素的时间结束时,它执行的程序元素中(和然后,一旦执行该程序,解析器移动到后面的SCRIPT元素的HTML code)的

A ReferenceError will be thrown ("foo is not defined").This leads to the conclusion that every SCRIPT element inside the HTML code of the web-page represents a separate JavaScript program and every time the HTML parser encounters a SCRIPT element, it executes the program inside that element (and then once the program is executed, the parser moves on to the HTML code that follows the SCRIPT element).

然后,这种确实工作

<script>
    function foo() {
        return;
    }
</script>
<script>
    foo();
</script>

在这里,我的理解是,全局对象(其作为在全球执行上下文的变量对象)是否存在(并保持),在任何时候,所以第一个JavaScript程序将创建函数对象,并为它的引用,然后第二JavaScript程序将使用该参照调用该函数。因此,所有的JavaScript程序(一个网页内)利用相同的全局对象,并且由一个JavaScript程序做全局对象的所有变化可以通过随后运行所有的JavaScript程序可以观察到。

My understanding here is that the Global object (which serves as the Variable object in the global execution context) exists (and remains) at all times, so the first JavaScript program will create the function object and make a reference for it, and then the second JavaScript program will use that reference to call the function. Therefore, all JavaScript programs (within a single web-page) "use" the same Global object, and all changes done to the Global object by one JavaScript program can be observed by all JavaScript programs that run subsequently.

现在,注意,这个...

Now, note this...

<script>
    // assuming that foo is not defined
    foo();
    alert(1);
</script>

在上述情况下,报警电话的不会执行后,因为富()语句抛出的ReferenceError(它打破了整个JavaScript程序),因此,所有的后续语句不执行

In the above case, the alert call will not execute, because the "foo()" statement throws a ReferenceError (which breaks the whole JavaScript program) and therefore, all subsequent statements do not execute.

然而,在这种情况下,...

However, in this case...

<script>
    // assuming that foo is not defined
    foo();
</script>
<script>
    alert(1);
</script>

现在,报警电话的不会被执行。第一个JavaScript程序抛出的ReferenceError(并因此中断),但第二个JavaScript程序正常运行。当然,浏览器会报告错误(虽然它执行后续的JavaScript程序,错误发生后)。

Now, the alert call does get executed. The first JavaScript program throws a ReferenceError (and as a consequence breaks), but the second JavaScript program runs normally. Of course, the browser will report the error (although it did execute subsequent JavaScript programs, after the error occurred).

现在,我的结论是:


  • 的网页重新$ P $的HTML code中的每个SCRIPT元素psents一个独立的JavaScript程序。这些程序立即执行的HTML解析器遇到他们。

  • 在同一网页中的所有JavaScript程序用同一个全局对象。该全局对象存在在所有时间(从网页中提取直到网页被破坏的时刻)。 JavaScript程序可以操纵全局对象,并且由一个JavaScript程序做全局对象的所有变化可以在随后的所有JavaScript程序被观察到。

  • 如果一个JavaScript程序中断(通过其引发的错误),一点不为prevent随后的JavaScript程序执行。

请其实检查这篇文章,并告诉我,如果我得到了什么。

Please fact-check this post and tell me if I got something wrong.

另外,我还没有发现,说明在这个帖子中提到的行为的资源,我认为浏览器厂商一定是什么地方发表过这样的资源,所以,如果你了解他们,请提供链接到他们。

Also, I have not found resources that explain the behaviors mentioned in this post, and I assume that the browser makers must have published such resources somewhere, so if you know about them, please provide the links to them.

更新!

OK,我要(尽量)在这里回答我的问题:)
我得到了(通过电子邮件)从梅德A. Soshnikov(他经营着一个关于JavaScript的博客在)。

OK, I am going to (try to) answer my own question here :)I got a response (via e-mail) from Dmitry A. Soshnikov (he runs a blog about JavaScript at http://www.dmitrysoshnikov.com/ ).

他在这个问题上是这样的:每个脚本块包含全球code。执行每个脚本块创建一个新的执行上下文。因此,每个脚本块都有自己的执行上下文,但所有这些执行上下文共享同一个全局对象。

His take on this issue is this: Each SCRIPT block contains global code. Executing each SCRIPT block creates a new execution context. Therefore, each SCRIPT block has its own execution context, but all those execution contexts share the same Global object.

SCRIPT块可以被看作是不同的子计划具有相同的共享状态。

SCRIPT blocks could be viewed as different "sub-programs" with the same shared state.

此外,规范的ECMAScript(第三版)状态(第10章):
全球code是被视为一个ECMAScript程序的源文本。

Furthermore, the ECMAScript spec (3rd edition) states (chapter 10):"Global code is source text that is treated as an ECMAScript Program."

推荐答案

梅德Soshnikov已经回答了你的问题。每个&LT;脚本&GT; 元素作为程序执行,由ECMAScript规范的定义。有一个单一的页中的每个程序使用一个全局对象。这是真正的。

Dmitry Soshnikov has answered your question. Every <script> element is executed as a Program, as defined by the ECMAScript specification. There is one global object that each Program within a single page uses. And that's really it.

这篇关于有多少JavaScript程序在浏览器中一个单一的网页执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:12