age
, the multiplier
function, and the result
variable are in the external environment. These components have a global scope. A scope is the area in which a component is available.x
is a constant inside the multiplier
function. Since it is inside a block of code, it is a local constant, not a global one. It is visible only inside the function, but not outside - its scope is local.multiplier
function has another component from the local scope - this is the num
argument. It is more difficult to define it than a constant or a variable, but it behaves approximately like a local variable.x
- it does not seem to exist:console.log
called x
in a global environment in which it is not defined. As a result, we got a ReferenceError.x
globally:x
with a known value, but the local x
in the multiplier
function is still visible only from the inside. These x
are not connected in any way - they are in different environments. Despite the same name, they do not mix.if
:while
and for
loops.a
has changed inside the changer
function. The function takes effect only when called, not when defined, therefore initially a=0
, and when called, changer
takes the value 1
.multiplier
function returns the results of a
and b
. a
given inside, but b
not.a*b
multiplication operation, JavaScript looks for the values ​​of a
and b
. He begins to search inside, and then goes outside, studying one area after another, until he finds what he was looking for, or does not understand that it is impossible to find.a
inside the local area - inside the multiplier
function. He immediately finds the value and goes to b
. b
he will not find in the local environment, so he goes beyond it. There he learns that b
is 10
. So a*b
turns into 5*10
, and then to 50
.b
in the first layer, JavaScript would continue to search everything in new and new layers, further and further.a=7
does not affect the calculation result in any way: the value of a
was found inside, therefore, the external a
does not play a
role.f
is a pretty useless function that always returns 0
. The set consists of two parts - a constant and the function itself.f
. Its value can be a number or a string value. In this case, the value is a function.f
is a piece of paper with f
written on one side and a description of the function being launched on the other.createPrinter
function creates a name
constant, and then a function called printName
. Both are local to the createPrinter
function and are available only inside it.printName
itself has printName
local components, but there is access to the scope where it is created, and to the external environment, where the constant is assigned a name
.createPrinter
function returns the printName
function. Recall that function definitions are descriptions of running functions; they are simply data elements, like numbers or chains. Therefore, we can return the definition of a function in the same way as we return numbers.myPrinter
and set the return value to it as createPrinter
. A function is returned, so myPrinter
also becomes a function. When you call it, King
will appear on the screen.name
constant was created inside the createPrinter
function. The function was called and executed. As you know, when a function finishes working, it ceases to exist. The magic box disappears along with all the contents.name
constant. Thus, when calling myPrinter
we get King
- the value that the function remembers, despite the fact that this area no longer exists.createPrinter
returns is called a closure. A closure is a combination of a function and the environment in which it was defined. The function "enclosed" in itself certain information received in the scope.Source: https://habr.com/ru/post/460861/
All Articles