2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244be
title: Global Scope and Functions
challengeType: 1
2019-08-28 16:26:13 +03:00
videoUrl: https://scrimba.com/c/cQM7mCN
forumTopicId: 18193
2018-10-10 18:03:03 -04:00
localeTitle: Глобальная область и функции
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
В JavaScript < dfn > область видимости< / dfn > относится к видимости переменных. Переменные, определенные вне функционального блока, имеют < dfn > глобальную< / dfn > область. Это означает, что их можно увидеть везде в вашем JavaScript-коде. Переменные, которые используются без ключевого слова < code > var< / code > , автоматически создаются в < code > global< / code > области. Это может привести к непредвиденным последствиям в другом месте вашего кода или при повторном запуске функции. Вы всегда должны объявлять переменные с помощью < code > var< / code > .
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
Используя < code > var< / code > , объявляйте < code > global< / code > переменную < code > myGlobal< / code > вне любой функции. Инициализируйте е г о с о значением < code > 10< / code > . Внутри функции < code > fun1< / code > присвойте < code > 5< / code > < code > oopsGlobal< / code > < strong > < em > без< / em > < / strong > использования ключевого слова < code > var< / code > .
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > myGlobal</ code > should be defined
testString: assert(typeof myGlobal != "undefined");
- text: < code > myGlobal</ code > should have a value of < code > 10</ code >
testString: assert(myGlobal === 10);
- text: < code > myGlobal</ code > should be declared using the < code > var</ code > keyword
testString: assert(/var\s+myGlobal/.test(code));
- text: < code > oopsGlobal</ code > should be a global variable and have a value of < code > 5</ code >
testString: assert(typeof oopsGlobal != "undefined" & & oopsGlobal === 5);
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Declare your variable here
function fun1() {
// Assign 5 to oopsGlobal Here
}
// Only change code above this line
function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
```
< / div >
2019-08-28 16:26:13 +03:00
### Before Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-setup' >
```js
var logOutput = "";
var originalConsole = console
function capture() {
var nativeLog = console.log;
console.log = function (message) {
logOutput = message;
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
}
function uncapture() {
console.log = originalConsole.log;
}
var oopsGlobal;
capture();
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
fun1();
fun2();
uncapture();
(function() { return logOutput || "console.log never called"; })();
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
// Declare your variable here
var myGlobal = 10;
function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;
}
// Only change code above this line
function fun2() {
var output = "";
if(typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if(typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >