2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7db2367417b2b2512b8b
|
|
|
|
|
challengeType: 1
|
2020-08-04 15:15:28 +08:00
|
|
|
|
forumTopicId: 301328
|
|
|
|
|
localeTitle: 了解立即调用函数表达(IIFE)
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:15:28 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
JavaScript 中的一个常见模式就是,函数在声明后立刻执行:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(function () {
|
|
|
|
|
console.log("Chirp, chirp!");
|
|
|
|
|
})(); // 这是一个立即执行的匿名函数表达式
|
|
|
|
|
// 立即输出 "Chirp, chirp!"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
请注意,函数没有名称,也不存储在变量中。函数表达式末尾的两个括号()导致它被立即执行或调用。这种模式被叫做<code>自执行函数表达式</code>或者<code>IIFE</code>。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:15:28 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
重写函数<code>makeNest</code>,并删除它的调用,取而代之是一个匿名的<code>自执行函数表达式</code>(<code>IIFE</code>)。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-08-04 15:15:28 +08:00
|
|
|
|
- text: 该函数应该是匿名的。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/\((function|\(\))(=>|\(\)){/.test(code.replace(/\s/g, "")));
|
2020-08-04 15:15:28 +08:00
|
|
|
|
- text: 函数应该在表达式的末尾有括号,以便立即调用它。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(/}\)\(\)/.test(code.replace(/\s/g, "")));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function makeNest() {
|
|
|
|
|
console.log("A cozy nest is ready");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
makeNest();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-08-04 15:15:28 +08:00
|
|
|
|
(function () {
|
|
|
|
|
console.log("A cozy nest is ready");
|
|
|
|
|
})();
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|