JavaScript programs contain claims and function terms. Whenever a JavaScript program is performed, both of these steps occur:

  1. the code is scanned for function terms, and each func. declaration is "performed" (by developing a function object) along with a named mention of the that function is produced (to ensure that this function could be known as from inside an argument)

  2. the claims are performed (examined) sequentially (because they come in the code)

Due to that, this works all right:

<script>

    foo()

    function foo() 

</script>

Even though "foo" function is known as prior to it being declared, it really works since the function declaration is examined prior to the statement.

However, this doesn't work:

<script>

    foo()

</script>

<script>

    function foo() 

</script>

A ReferenceError is going to be tossed ("foo isn't defined"). This can lead to the final outcome that each SCRIPT element within the Html page from the web-page signifies another JavaScript program and each time the HTML parser encounters a SCRIPT element, it executes this program within that element (after which when the program is performed, the parser moves onto the Html page that follows the SCRIPT element).

On the other hand, this works:

<script>

    function foo() 

</script>

<script>

    foo()

</script>

My understanding here would be that the Global object (which can serve as the Variable object within the global execution context) is available (and stays) whatsoever occasions, therefore the first JavaScript program can create the function object making a reference for this, and so the second JavaScript program uses that mention of the call the function. Therefore, all JavaScript programs (inside a single web-page) "use" exactly the same Global object, and all sorts of changes completed to the worldwide object by one JavaScript program could be observed by all JavaScript programs running subsequently.

Now, note this...

<script>

    // presuming that foo isn't defined

    foo()

    alert(1)

</script>

Within the above situation, the alert call won't execute, since the "foo()" statement throws a ReferenceError (which breaks the entire JavaScript program) and for that reason, all subsequent claims don't execute.

However, within this situation...

<script>

    // presuming that foo isn't defined

    foo()

</script>

<script>

    alert(1)

</script>

Now, the alert call does get performed. The very first JavaScript program throws a ReferenceError (and as a result breaks), however the second JavaScript program runs normally. Obviously, the browser will report the mistake (even though it did execute subsequent JavaScript programs, following the error happened).

Now, my conclusions are:

  • every SCRIPT element inside the Html page from the web-page signifies another JavaScript program. These programs execute immediately because the HTML parser encounters them.
  • all JavaScript programs inside the same web-page "use" exactly the same Global object. That Global object is available whatsoever occasions (as soon as the net-page is fetched up to the net-page is destroyed). JavaScript programs may manipulate the worldwide object, and all sorts of changes completed to the worldwide object by one JavaScript program could be observed in most subsequent JavaScript programs.
  • if a person JavaScript program breaks (by getting a mistake tossed), that doesn't prevent subsequent JavaScript programs to complete.

Please fact-take a look publish and let me know basically got a problem.

Also, I haven't found assets that explain the actions pointed out within this publish, and that i think that the browser makers should have released such assets somewhere, if you learn about them, please supply the links for them.

UPDATE!

OK, I will (attempt to) answer my very own question here :) I acquired an answer (via e-mail) from Dmitry A. Soshnikov (he runs a website on JavaScript at http://world wide web.dmitrysoshnikov.com/ ).

His undertake this problem is: Each SCRIPT block consists of global code. Performing each SCRIPT block produces a brand new execution context. Therefore, each SCRIPT block features its own execution context, but all individuals execution contexts share exactly the same Global object.

SCRIPT blocks might be seen as different "sub-programs" with similar shared condition.

In addition, the ECMAScript spec (3rd edition) states (chapter 10): "Global code is source text that's treated being an ECMAScript Program."