1.7 KiB
1.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7db2367417b2b2512b8b | 即時実行関数式 (IIFE) を理解する | 1 | 301328 | understand-the-immediately-invoked-function-expression-iife |
--description--
JavaScript では、関数を宣言したらすぐに実行するというのが一般的なパターンです。
(function () {
console.log("Chirp, chirp!");
})();
これは無名関数式で、即座に実行され、すぐに Chirp, chirp!
を出力します。
関数には名前を付けず、変数に保存していないことに注意してください。 関数式の末尾にある 2 つの括弧 () により、直ちに実行または呼び出しをします。 このパターンのことを即時実行関数式または IIFE と呼びます。
--instructions--
関数 makeNest
を書き換え、その呼び出しを削除して、代わりに無名の即時実行関数式 (IIFE) にしてください。
--hints--
関数は無名である必要があります。
assert(/\((function|\(\))(=>|\(\)){?/.test(code.replace(/\s/g, '')));
関数の式の末尾に括弧を付けて、すぐに呼び出す必要があります。
assert(/\(.*(\)\(|\}\(\))\)/.test(code.replace(/[\s;]/g, '')));
--seed--
--seed-contents--
function makeNest() {
console.log("A cozy nest is ready");
}
makeNest();
--solutions--
(function () {
console.log("A cozy nest is ready");
})();
(function () {
console.log("A cozy nest is ready");
}());
(() => {
console.log("A cozy nest is ready");
})();
(() =>
console.log("A cozy nest is ready")
)();