Skip to content

Commit ed7bbcc

Browse files
committed
function/general
1 parent 8d1b081 commit ed7bbcc

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

doc/ru/function/general.md

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
1-
## Function Declarations and Expressions
1+
## Выражения и объявление функций
22

3-
Functions in JavaScript are first class objects, that means that they can be
4-
passed around like any other value. One common use of this feature is to pass
5-
an *anonymous function* as a callback to another, possibly asynchronous function.
3+
Функции в JavaScript тоже являются объектами (шок, сенсация) — следовательно, их можно передавать и присваивать точно так же, как и любой другой объект. Один из вариантов использования такой возможности — передача *анонимной функции* как функции обратного вызова в другую функцию — к примеру, для ассинхронных вызовов.
64

7-
### The `function` declaration
5+
### Объявление `function`
86

7+
// всё просто и привычно
98
function foo() {}
109

11-
The above function gets [hoisted](#function.scopes) before the execution of the
12-
program starts; thus, it is available *everywhere* in the scope it was *defined*
13-
in, even if called before the actual definition in the source.
10+
В следующем примере описанная функция [резервируется](#function.scopes) перед запуском всего скрипта; за счёт этого она доступна *в любом месте* кода, вне зависимости от того где она *определена* — даже если функция вызывается до её фактического объявления в коде.
1411

15-
foo(); // Works because foo was created before this code runs
12+
13+
foo(); // сработает, т.к. функция будет создана до выполнения кода
1614
function foo() {}
1715

18-
### The `function` expression
16+
### `function` как выражение
1917

2018
var foo = function() {};
2119

22-
This example assigns the unnamed and *anonymous* function to the variable `foo`.
20+
В этом примере безымянная и *анонимная* функция присваивается переменной `foo`.
2321

2422
foo; // 'undefined'
25-
foo(); // this raises a TypeError
23+
foo(); // вызовет TypeError
2624
var foo = function() {};
2725

28-
Due to the fact that `var` is a declaration, that hoists the variable name `foo`
29-
before the actual execution of the code starts, `foo` is already defined when
30-
the script gets executed.
26+
Так как в данном примере выражение `var` — это определение функции, переменная с именем `foo` будет заранее зарезервирована перед запуском скрипта (таким образом, `foo` уже будет определена во время его работы).
27+
28+
Но поскольку присвоения исполняются непосредственно во время работы кода, `foo` по умолчанию будет присвоено значение [`undefined`](#core.undefined) (до обработки строки с определением функции):
3129

32-
But since assignments only happens at runtime, the value of `foo` will default
33-
to [undefined](#core.undefined) before the corresponding code is executed.
30+
var foo; // переменная неявно резервируется
31+
foo; // 'undefined'
32+
foo(); // вызовет TypeError
33+
foo = function() {};
3434

35-
### Named function expression
35+
### Выражения с именованными фунциями
3636

37-
Another special case is the assignment of named functions.
37+
Существует еще ньюанс, касающийся именованных функций создающихся через присваивание:
3838

3939
var foo = function bar() {
40-
bar(); // Works
40+
bar(); // работает
4141
}
42-
bar(); // ReferenceError
42+
bar(); // получим ReferenceError
4343

44-
Here `bar` is not available in the outer scope, since the function only gets
45-
assigned to `foo`; however, inside of `bar` it is available. This is due to
46-
how [name resolution](#function.scopes) in JavaScript works, the name of the
47-
function is *always* made available in the local scope of the function itself.
44+
Здесь объект `bar` не доступен во внешней области, так как имя `bar` используется только для присвоения переменной `foo`; однако `bar` можно вызвать внутри функции. Такое поведение связано с особенностью работы JavaScript с [пространствами имен](#function.scopes) - имя функции *всегда* доступно в локальной области видимости самой функции.
4845

0 commit comments

Comments
 (0)