You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/05-global-object/article.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,44 +14,44 @@ alert("Hello");
14
14
window.alert("Hello");
15
15
```
16
16
17
-
We can reference other built-in functions like `Array` as `window.Array` and create our own properties on it.
17
+
Мы можем ссылаться на другие встроенные функции типа `Array` как `window.Array` или создавать свои собственные свойства глобального объекта. We can reference other built-in functions like `Array` as `window.Array` and create our own properties on it.
18
18
19
-
## Browser: the "window" object
19
+
## Браузер: объект "window" Browser: the "window" object
20
20
21
-
For historical reasons, in-browser `window` object is a bit messed up.
21
+
По историческим причинам, браузерный объект `window` выглядит слегка запутанно. For historical reasons, in-browser `window` object is a bit messed up.
22
22
23
-
1. It provides the "browser window" functionality, besides playing the role of a global object.
23
+
1.Он предоставляет инструменты для работы с окном браузера, а также играет роль глобального объекта. It provides the "browser window" functionality, besides playing the role of a global object.
24
24
25
-
We can use `window` to access properties and methods, specific to the browser window:
25
+
Мы можем использовать `window` для доступа к свойствам и методам характерным для окна браузера. We can use `window` to access properties and methods, specific to the browser window:
26
26
27
27
```js run
28
-
alert(window.innerHeight); // shows the browser window height
28
+
alert(window.innerHeight); //выводит высоту окна браузера shows the browser window height
29
29
30
-
window.open('http://google.com'); // opens a new browser window
30
+
window.open('http://google.com'); //открывает новое окно браузера opens a new browser window
31
31
```
32
32
33
-
2. Top-level `var` variables and function declarations automatically become properties of `window`.
33
+
2.Если переменные, объявленные с помощью `var` или декларации функиций находятся на верхнем уровне программы, то они автоматически становятся свойствами `window`. Top-level `var` variables and function declarations automatically become properties of `window`.
34
34
35
-
For instance:
35
+
Например For instance:
36
36
```js untrusted run no-strict refresh
37
37
var x = 5;
38
38
39
-
alert(window.x); // 5 (var x becomes a property of window)
39
+
alert(window.x); // 5 (var x становиться свойством window) 5 (var x becomes a property of window)
Please note, that doesn't happen with more modern `let/const` declarations:
46
+
Обратите внимание, что этого не происходит с более современными `let/const` объявлениями. Please note, that doesn't happen with more modern `let/const` declarations:
47
47
48
48
```js untrusted run no-strict refresh
49
49
let x = 5;
50
50
51
-
alert(window.x); // undefined ("let" doesn't create a window property)
51
+
alert(window.x); // undefined ("let" не создаёт свойства window) undefined ("let" doesn't create a window property)
52
52
```
53
53
54
-
3. Also, all scripts share the same global scope, so variables declared in one `<script>` become visible in another ones:
54
+
3. Кроме того, все скрипты имеют общюю глобальную область видимотси, поэтому переменные, объявленные в одном теге `<script>` становятся видимыми в других. Also, all scripts share the same global scope, so variables declared in one `<script>` become visible in another ones:
55
55
56
56
```html run
57
57
<script>
@@ -65,21 +65,21 @@ For historical reasons, in-browser `window` object is a bit messed up.
65
65
</script>
66
66
```
67
67
68
-
4. And, a minor thing, but still: the value of `this` in the global scope is `window`.
68
+
4. Мелочь, но всё же, значение `this` в глобальной области видимости — `window`. And, a minor thing, but still: the value of `this` in the global scope is `window`.
69
69
70
70
```js untrusted run no-strict refresh
71
71
alert(this); // window
72
72
```
73
73
74
-
Why was it made like this? At the time of the language creation, the idea to merge multiple aspects into a single `window` object was to "make things simple". But since then many things changed. Tiny scripts became big applications that require proper architecture.
74
+
Почему было сделано так? На момент создания языка, объединение нескольких аспектов в одном объекте `window`, должно было "упростить" работу. Но с тех пор многое изменилось. Крошечные скрипты превратились в крупные приложения, требующие правльной архитектуры. Why was it made like this? At the time of the language creation, the idea to merge multiple aspects into a single `window` object was to "make things simple". But since then many things changed. Tiny scripts became big applications that require proper architecture.
75
75
76
-
Is it good that different scripts (possibly from different sources) see variables of each other?
76
+
Хорошо ли, что разные скрипты (возможно из разных источников) "видят" переменные друг друга? Is it good that different scripts (possibly from different sources) see variables of each other?
77
77
78
-
No, it's not, because it may lead to naming conflicts: the same variable name can be used in two scripts for different purposes, so they will conflict with each other.
78
+
Нет, потому что это может привести к конфликту имён: одинаковые имена переменных могут использоваться двумя скриптами для различных целей и эти переменные будут конфликтовать между собой. No, it's not, because it may lead to naming conflicts: the same variable name can be used in two scripts for different purposes, so they will conflict with each other.
79
79
80
-
As of now, the multi-purpose `window` is considered a design mistake in the language.
80
+
Сейчас, многоцелевой `window` считается ошибкой проектирования языка. As of now, the multi-purpose `window` is considered a design mistake in the language.
81
81
82
-
Luckily, there's a "road out of hell", called "JavaScript modules".
82
+
К счастью, есть "дорога из ада" — JavaScript модули. Luckily, there's a "road out of hell", called "JavaScript modules".
83
83
84
84
If we set `type="module"` attribute on a `<script>` tag, then such script is considered a separate "module" with its own top-level scope (lexical environment), not interfering with `window`.
0 commit comments