How is JavaScript possible?

How JavaScript performs an upgrade if it is an interpreted language. Non-interpreted languages ​​execute code from top to bottom. Is there any type of compilation running in the background?

+4
source share
3 answers

"Interpreted" does not mean that each line is executed immediately when it is read. The Javascript interpreter first reads the entire file, a process during which it parses information into executable code. Here comes the rise: between parsing and execution.


In a nutshell and very simplistic, lifting work:

  • the analyzer / interpreter reads the source code into an intermediate representation
  • (, function), *
  • var ( ), ( /undefined)

* "" , Javascript, , Javascript .

, , , . , "top", - .

+4

/ script . , " " "script ", , . , script .

functions classes loops, execupte .

+1

script , ...

var x = 5

var x, x.

, x 5

, JavaScript.


, JS :

  • Function declaration: (the function is defined by the keyword function), so it is on the left side and will be raised.
  • Functional expression: (the function is determined by the function assignment of the variable), so the variable will only be raised, and the assignment will be in the second phase.

So show that in action:

// This example will work
test();
function test () {
    alert('test');
}

and

// This example will raise an error
test();
var test = function () {
    alert();
}

Because in the second example it testwill be equal undefined, because the function has not yet been assigned to a variable test.

-1
source

Source: https://habr.com/ru/post/1652837/


All Articles