2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244bf
title: Local Scope and Functions
challengeType: 1
2019-08-28 16:26:13 +03:00
videoUrl: https://scrimba.com/c/cd62NhM
forumTopicId: 18227
2018-10-10 18:03:03 -04:00
localeTitle: Локальная область и функции
---
## Description
2019-08-28 16:26:13 +03:00
<section id='description'>
Переменные, объявленные внутри функции, а также параметры функции имеют <dfn>локальную</dfn> область. Это означает, что они видны только внутри этой функции. Вот функция <code>myTest</code> с локальной переменной <code>loc</code> . <blockquote> function myTest () { <br> var loc = "foo"; <br> console.log (LOC); <br> } <br> MyTest (); // logs "foo" <br> console.log (LOC); // loc не определен </blockquote> <code>loc</code> не определяется вне функции.
</section>
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
<section id='instructions'>
Объявите локальную переменную <code>myVar</code> внутри <code>myLocalScope</code> . Запустите тесты, а затем следуйте инструкциям, прокомментированным в редакторе. <strong>намек</strong> <br> Обновление страницы может помочь, если вы застряли.
</section>
2018-10-10 18:03:03 -04:00
## Tests
<section id='tests'>
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: No global <code>myVar</code> variable
testString: assert(typeof myVar === 'undefined');
- text: Add a local <code>myVar</code> variable
testString: assert(/function\s+myLocalScope\s*\(\s*\)\s*\{\s[\s\S]+\s*var\s*myVar\s*(\s*|=[\s\S]+)\s*;[\s\S]+}/.test(code));
2018-10-10 18:03:03 -04:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function myLocalScope() {
'use strict'; // you shouldn't need to edit this line
console.log(myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);
// Now remove the console log line to pass the test
```
</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;
}
```
</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
typeof myLocalScope === 'function' && (capture(), myLocalScope(), 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
function myLocalScope() {
'use strict';
var myVar;
console.log(myVar);
}
myLocalScope();
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>