Skip to content

Commit 9e50e31

Browse files
authored
Translate the article. Vol. 3
1 parent 95731e7 commit 9e50e31

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

  • 1-js/06-advanced-functions/05-global-object

1-js/06-advanced-functions/05-global-object/article.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ window.alert("Hello");
8181

8282
К счастью, есть "дорога из ада" — JavaScript модули. Luckily, there's a "road out of hell", called "JavaScript modules".
8383

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`.
84+
Если мы установим тегу `<script>` атрибут `type="module"`, такой скрипт будет считаться отдельным модулем сосбственной областью видимости верхнего уровня (лексическим окружением) не пересекающейся с `window`. 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`.
8585

86-
- In a module, `var x` does not become a property of `window`:
86+
- В модуле `var x` не станет свойством `window`: - In a module, `var x` does not become a property of `window`:
8787

8888
```html run
8989
<script type="module">
@@ -93,7 +93,7 @@ If we set `type="module"` attribute on a `<script>` tag, then such script is con
9393
</script>
9494
```
9595

96-
- Two modules that do not see variables of each other:
96+
- Два модуля, которые не видят переменные друг друга: - Two modules that do not see variables of each other:
9797

9898
```html run
9999
<script type="module">
@@ -102,54 +102,54 @@ If we set `type="module"` attribute on a `<script>` tag, then such script is con
102102

103103
<script type="module">
104104
alert(window.x); // undefined
105-
alert(x); // Error: undeclared variable
105+
alert(x); // Ошибка: переменная не объявлена Error: undeclared variable
106106
</script>
107107
```
108108

109-
- And, the last minor thing, the top-level value of `this` in a module is `undefined` (why should it be `window` anyway?):
109+
- И последнее, в модуле значение `this` на верхнем уровне равно `undefined` (действительно, почему должно быть `window`?): - And, the last minor thing, the top-level value of `this` in a module is `undefined` (why should it be `window` anyway?):
110110

111111
```html run
112112
<script type="module">
113113
alert(this); // undefined
114114
</script>
115115
```
116116

117-
**Using `<script type="module">` fixes the design flaw of the language by separating top-level scope from `window`.**
117+
**Используйте `<script type="module">`, чтобы исправить недостаток проектирования языка, отделяя область видимости верхнего уровня от `window`. Using `<script type="module">` fixes the design flaw of the language by separating top-level scope from `window`.**
118118

119-
We'll cover more features of modules later, in the chapter [](info:modules).
119+
Мы рассмотрим другие свойства модулей позже в главе We'll cover more features of modules later, in the chapter [](info:modules).
120120

121-
## Valid uses of the global object
121+
## Допустимое (правильное?) использование глобального объекта Valid uses of the global object
122122

123-
1. Using global variables is generally discouraged. There should be as few global variables as possible, but if we need to make something globally visible, we may want to put it into `window` (or `global` in Node.js).
123+
1. Обычно не рекомендуется использовать глобальные переменные. Желатьльно, применать как можно мешьше таких переменных. Однако, если нужно поместить что-либо в глобальную область видимости, мы можем захотеть добавить это в `window` (или `global` в Node.js) Using global variables is generally discouraged. There should be as few global variables as possible, but if we need to make something globally visible, we may want to put it into `window` (or `global` in Node.js).
124124

125-
Here we put the information about the current user into a global object, to be accessible from all other scripts:
125+
Мы помещаем информацию о текущем пользователе в глобальный объект, чтобы она была доступна другим скриптам. Here we put the information about the current user into a global object, to be accessible from all other scripts:
126126

127127
```js run
128-
// explicitly assign it to `window`
128+
// явное назначение свойства `window` explicitly assign it to `window`
129129
window.currentUser = {
130130
name: "John",
131131
age: 30
132132
};
133133

134-
// then, elsewhere, in another script
134+
// далее, где угодно в другом скрипте then, elsewhere, in another script
135135
alert(window.currentUser.name); // John
136136
```
137137

138-
2. We can test the global object for support of modern language features.
138+
2. Мы можем проверить, поддерживает ли глобальный объект современные возможности языка. We can test the global object for support of modern language features.
139139

140-
For instance, test if a build-in `Promise` object exists (it doesn't in really old browsers):
140+
Например, проверить наличие встроенного объекта `Promise` (такая поддержка отсутствует в очень старых браузерах). For instance, test if a build-in `Promise` object exists (it doesn't in really old browsers):
141141
```js run
142142
if (!window.Promise) {
143-
alert("Your browser is really old!");
143+
alert("Ваш браузер очень старый!"); alert("Your browser is really old!");
144144
}
145145
```
146146
147-
3. We can create "polyfills": add functions that are not supported by the environment (say, an old browser), but exist in the modern standard.
147+
3. Мы можем создать полифилл: добавить функции, которые не поддерживаются окружением (скажем, старым браузером), но сущемтвуют в современном стандарте. We can create "polyfills": add functions that are not supported by the environment (say, an old browser), but exist in the modern standard.
148148
149149
```js run
150150
if (!window.Promise) {
151-
window.Promise = ... // custom implementation of the modern language feature
151+
window.Promise = ... // собственная реализация современной возможности языка custom implementation of the modern language feature
152152
}
153153
```
154154
155-
...And of course, if we're in a browser, using `window` to access browser window features (not as a global object) is completely fine.
155+
...И конечно, если в браузере мы используем `window` для доступа к функциям окна браузера (не как глобального объекта) это вполне нормально. And of course, if we're in a browser, using `window` to access browser window features (not as a global object) is completely fine.

0 commit comments

Comments
 (0)