logo

JavaScript 迭代器与生成器

✏️ Editable
Loading...
Live Preview
+Enter Run+R Reset

About This Exercise

生成器(generator)可以理解成“按需发货机”:你取一次,它产出一个值。

这在大列表、分页数据、数据流处理中很常见。

本关只练三件事:基础产出、基于产出筛选、按步长产出。

Intermediate⏱ 20-25 min

Learning Objectives

  • 掌握 function* 与 yield 的基础语法
  • 学会把生成器结果转成数组使用
  • 理解按需产出的性能意义

Scenario

你在处理分页或流式数据,希望按顺序逐个拿值,再做筛选和步长控制。 这正是 generator 的典型场景。

Why Do This

  • Frontend JS is not just about knowing the syntax — the key is reliably manipulating the DOM and managing interaction state.
  • Break down the event flow (trigger -> handle -> update UI) before coding, and you will make significantly fewer errors.
  • Rule-based validation helps you develop a testable frontend mindset.

Hands-on Practice

  • Write out the event flow in comments first, then implement the functions.
  • Add an edge case branch for invalid input or empty data.
  • Explain why each validation rule is needed.

Common Mistakes

  • Only testing the happy path without verifying edge inputs or repeated clicks.
  • Mixing event binding and state updates together, making future maintenance difficult.
  • Console output looks correct but the UI is not actually updated.

Deliverable

A reusable interaction component scaffold (event flow description + key functions + edge case handling).

My Progress

Completed Steps0 / 3
Total Attempts0
Best Score0%

Completion Criteria (Checkable)

Confirm before completing this Lab

Reflection Questions (Recommended)

Describe the event flow of this Lab in 2-3 sentences (trigger -> handle -> update).

What edge case did you add? Why is it easy to miss?

How confident are you with this interaction pattern now?

Tags

JavaScriptES6IteratorGenerator