freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/object-oriented-programming/understand-the-immediately-invoked-function-expression-iife.chinese.md
ZhichengChen 303e228299
fix(i18n): update Chinese i18n of object oriented programming (#38055)
Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
2020-08-04 12:45:28 +05:30

1.5 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7db2367417b2b2512b8b Understand the Immediately Invoked Function Expression (IIFE) 1 301328 了解立即调用函数表达IIFE

Description

JavaScript 中的一个常见模式就是,函数在声明后立刻执行:
(function () {
  console.log("Chirp, chirp!");
})(); // 这是一个立即执行的匿名函数表达式
// 立即输出 "Chirp, chirp!"

请注意,函数没有名称,也不存储在变量中。函数表达式末尾的两个括号()导致它被立即执行或调用。这种模式被叫做自执行函数表达式或者IIFE

Instructions

重写函数makeNest,并删除它的调用,取而代之是一个匿名的自执行函数表达式IIFE)。

Tests

tests:
  - text: 该函数应该是匿名的。
    testString: assert(/\((function|\(\))(=>|\(\)){/.test(code.replace(/\s/g, "")));
  - text: 函数应该在表达式的末尾有括号,以便立即调用它。
    testString: assert(/}\)\(\)/.test(code.replace(/\s/g, "")));

Challenge Seed

function makeNest() {
  console.log("A cozy nest is ready");
}

makeNest();

Solution

(function () {
  console.log("A cozy nest is ready");
})();