On learn.js it is written:

The execution of the script occurs in two phases:

In the first phase, initialization takes place, preparing for launch.

During initialization, the script is scanned for the declaration of functions of the type Function Declaration, and then for the declaration of var variables. Each such ad is added to the window.

Functions declared as Function Declaration are created immediately, and variables equal to undefined.

In the second phase - actually, implementation.

The assignment (=) of variable values ​​occurs when the execution thread reaches the corresponding line of code, until then they are undefined.

And I also answered stackoverflow that:

The work of any script in javascript consists of two phases. The first is the parser, which looks at all the input text and prepares the script for execution, translates it into an internal representation. In the second phase, this internal representation begins to run.

  1. Is it true that we are talking about the same phases?
  2. If yes, then in the first phase not only the parser works, but initialization also occurs?
  3. Bulk please, what phases are there at all? And what happens during each phase?
  • one
    Runtime Semantics: ScriptE evaluationJob From the algorithm it can be seen that ParseScript is first executed, and then, if there were no errors, ScriptEvaluation - Grundy pm
  • You can even divide into 10 phases - it depends on which side you look at. If you are interested in a cap - then js compile jit compilers in modern browsers, and then execute. And in general, and there and there it is written correctly, and about the same thing, except that these “phases” are carried out with each script in turn (as a rule, there are a lot of scripts connected) - Alexander Goncharov

2 answers 2

You write yourself:

The work of any script in javascript consists of two phases. The first is the parser , which looks at all the input text and prepares the script for execution, translates it into an internal representation. In the second phase, this internal representation begins to run .

Script execution occurs in two phases.


So.

  1. Is it true that we are talking about the same phases?

Not. One phase of parsing and two more execution phases.

  1. If yes, then in the first phase not only the parser works, but initialization also occurs?

No, It is Immpossible.

First, the script may not execute at all if there is a syntax error in it:

 try { console.log(never executed) } catch (e) { console.log("And this is never executed too") } 

Secondly, all variables belong to the executing function and each of its launch generates a new set of variables. And when parsing, the function is not executed at all. Perhaps it will never be fulfilled.

 function f() { var a = Math.random() return () => a } var f1 = f(), f2 = f() console.log(f1(), f2()) // Они разные 

  1. Bulk please, what phases are there at all? And what happens during each phase?

It seems like it all.


And something else:

Each such ad is added to the window.

window here only for code located directly on the top level (not in functions).

  • thanks, clarified something. It turns out there is a phase of parsing and a phase of implementation (which is divided into two phases)? Could you just have more details about the two phases of implementation? - Topik
  • Answer plizzz .. - Topik
  • @Topik, what exactly to answer? - Qwertiy
  • on my first comment. He addressed to you. - Topik
  • @Topik, the first question is yes. The second - everything seems to be in the text given. - Qwertiy 4:14 pm
  1. It is about different phases. The first definition describes the behavior of variables in the phases of script execution. The second definition describes the internal mechanism for implementing a specific runtime. Both phases from the first definition occur in the second phase of the second (implementation).

  2. No, in the first phase of the first definition, most of the runtime parser has finished its work and the compiled code is being executed.

  3. Any process can be divided into some phases depending on the purpose of this division. From the first definition we can understand what will happen if we refer to a variable before its description in var. From the second one, get some idea of ​​the internal work of the runtime (for example, understand that the entire script will not be executed if the parser even rejects its last line).