3.2 KiB
3.2 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244be | グローバルスコープと関数 | 1 | https://scrimba.com/c/cQM7mCN | 18193 | global-scope-and-functions |
--description--
JavaScript では、変数の効力の及ぶ範囲のことをスコープと呼びます。 関数ブロックの外側で定義された変数は、グローバルのスコープを持ちます。 つまり、JavaScript コードのどこからでもその変数を参照することができます。
let
キーワードまたは const
キーワードを使用せずに宣言された変数は、自動的に global
スコープで作成されます。 これは、コード内の他の場所で、または関数を再び実行するときに、意図しない結果を引き起こす可能性があります。 変数は常に let
または const
で宣言するようにしてください。
--instructions--
let
または const
を使用して、関数の外部で myGlobal
という名前のグローバル変数を宣言し、 値 10
で初期化してください。
関数 fun1
の内側で、let
キーワードまたは const
キーワードを使用せずに、oopsGlobal
に 5
を代入してください。
--hints--
myGlobal
を定義する必要があります。
assert(typeof myGlobal != 'undefined');
myGlobal
の値は 10
である必要があります。
assert(myGlobal === 10);
myGlobal
は let
キーワードまたは const
キーワードを使用して宣言する必要があります。
assert(/(let|const)\s+myGlobal/.test(code));
oopsGlobal
はグローバル変数で、値は 5
である必要があります。
assert(typeof oopsGlobal != 'undefined' && oopsGlobal === 5);
--seed--
--before-user-code--
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();
--after-user-code--
fun1();
fun2();
uncapture();
(function() { return logOutput || "console.log never called"; })();
--seed-contents--
// Declare the myGlobal variable below this line
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);
}
--solutions--
const myGlobal = 10;
function fun1() {
oopsGlobal = 5;
}
function fun2() {
var output = "";
if(typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if(typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}