2.9 KiB
2.9 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
id | title | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244bf | Local Scope and Functions | 1 | https://scrimba.com/c/cd62NhM | 18227 | Локальная область и функции |
Description
myTest
с локальной переменной loc
. function myTest () {
var loc = "foo";
console.log (LOC);
}
MyTest (); // logs "foo"
console.log (LOC); // loc не определен
loc
не определяется вне функции.
Instructions
myVar
внутри myLocalScope
. Запустите тесты, а затем следуйте инструкциям, прокомментированным в редакторе. намек Обновление страницы может помочь, если вы застряли.
Tests
tests:
- 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));
Challenge Seed
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
Before Tests
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;
}
After Tests
typeof myLocalScope === 'function' && (capture(), myLocalScope(), uncapture());
(function() { return logOutput || "console.log never called"; })();
Solution
function myLocalScope() {
'use strict';
var myVar;
console.log(myVar);
}
myLocalScope();