The mechanism of operation of functions and variables in JavaScript is very different from most languages.
To understand it, we will look at variables and functions in the global scope in this chapter. And in the next - let's go further.
Global object
Global are variables and functions that are not inside a function. That is, in other words, if a variable or function is not inside a function
construct, then they are “global”.
In JavaScript, all global variables and functions are properties of a special object called a global object
.
In the browser, this object is clearly available under the name window
. The window
object is also a global object and contains a number of properties and methods for working with a browser window, but we are only interested in its role as a global object.
In other environments, such as Node.JS, the global object may not be available explicitly, but the essence of what is happening from this does not change, therefore, we will use "window"
to denote the global object.
Assigning or reading a global variable, we actually work with the window
properties.
For example:
You can create a variable by explicit assignment in a window
:
Initialization order
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 in the second phase, when the execution flow reaches the corresponding line of code.
At the beginning of the code below is the content of the global object at the time of the initialization:
8 | var g = function (arg) { }; |
By the way, the fact that by the beginning of the code execution variables and functions are already contained in the window
can be easily checked:
In the old JavaScript standard, a variable could be created without a var
declaration:
Such an assignment, like var a = 5
, creates the window.a = 5
property. The difference from var a = 5
is that the variable will be created not at the stage of entering the scope, but at the moment of assignment.
Compare the two codes below.
The first one will display undefined
, since the variable was added to the window
during the initialization phase:
The second code will print an error, because the variable does not exist yet:
In general, it is recommended to always declare variables through var
.
In the modern standard, assignment without a var
will cause an error:
Curly brackets, which are used in for, while, if
, unlike function declarations, have a “decorative” character.
In JavaScript, there is no difference between a declaration outside of a block:
... And inside it:
There is also no difference between the declaration in the loop and outside it:
Identical in functionality code:
In both cases, the variable will be created before the loop is executed, at the initialization stage, and its value will be saved after the end of the loop.
var
ads can be any number of:
3 | for ( var i=0; i<20; i++) { |
All var
will be processed once, during the initialization phase.
During the execution phase, var
declarations will be ignored: they have already been processed. But assignments will be made.
In old IE, there are two funny errors when working with variables in a window
:
- Redefining a variable with the same name as the element
id
will result in an error: And if done through var
, then everything will be fine.
It was an advertisement for putting var
everywhere.
- An error occurred during recursion through the
window
property function. The following code will "die" in IE <9: The problem here arises from the fact that the function is directly assigned in window.recurse = ...
It will not be in the normal function declaration.
This example will generate an error only in true IE8! Not IE9 in emulation mode. In general, the emulation mode allows you to catch somewhere 95% of incompatibilities and problems, and for the remaining 5% you will need a real IE8 in the virtual machine.
Total
As a result of initialization, to the beginning of the code execution:
- Functions declared as
Function Declaration
are completely created and ready to use. - Variables are declared but equal to
undefined
. Assignments will be executed later when execution reaches them.
Importance: 5
Answer: 1
.
Let's see why.
At the preparatory stage for execution, window.a
is created from var a
:
As a result, a
becomes 1
.
[Open task in new window]
Importance: 5
Answer: an error .
There is no variable a
, so the condition "a" in window
will not be fulfilled. As a result, the last line is a call to an undefined variable.
[Open task in new window]
Importance: 5
Answer: 1
.
The variable a
is created before the start of the code execution, so that the condition "a" in window
fulfilled and the a = 1
triggered.
[Open task in new window]
Importance: 3
Answer: 5
.
To understand why, let's look at how this code works.
- Before execution starts, the variable
a
and the function a
. The standard is written in such a way that the function is created first and the variable does not overwrite it. That is, the function takes precedence. But in this case it doesn't matter at all, because ... - ... After initialization, when the code starts to execute - assignment
a = 5
works, overwriting a
, and it does not matter what lay there. -
Function Declaration
declaration declaration at execution stage is ignored (already processed). - As a result,
alert(a)
displays 5.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone