freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../object-oriented-programming/understand-the-immediately-...

1.6 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db2367417b2b2512b8b Understand the Immediately Invoked Function Expression (IIFE) 1 理解立即调用的函数表达式IIFE

Description

JavaScript中的一个常见模式是在声明函数后立即执行
function{
console.log“Chirpchirp;
}; //这是一个立即执行的匿名函数表达式
//输出“Chirpchirp”立即
请注意,该函数没有名称,也没有存储在变量中。函数表达式末尾的两个括号()会立即执行或调用它。此模式称为immediately invoked function expressionIIFE

Instructions

重写函数makeNest并删除它的调用,所以它是一个匿名的immediately invoked function expression IIFE )。

Tests

tests:
  - text: 该功能应该是匿名的。
    testString: 'assert(/\(\s*?function\s*?\(\s*?\)\s*?{/.test(code), "The function should be anonymous.");'
  - text: 您的函数应在表达式的末尾加上括号以立即调用它。
    testString: 'assert(/}\s*?\)\s*?\(\s*?\)/.test(code), "Your function should have parentheses at the end of the expression to call it immediately.");'

Challenge Seed

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

makeNest();

Solution

// solution required