激情欧美日韩一区二区|国产18在线播放|黄的日本免费大片|国产色在线 | 亚洲|青青操夜夜操

Professional JavaScript for Web Developers 第四版學習筆記 CHAPTER 7: ITERATORS AND GENERATORS

歡歡歡歡 發表于 2021-4-5 08:42

Introduction to Iteration 225
The Iterator Pattern 226
The Iterable Protocol 227
The Iterator Protocol 229
Custom Iterator Definition 231
Early Termination of Iterators 233
Generators 236
Generator Basics 236
Interrupting Execution with “yield” 238
  Using a Generator Object as an Iterable 239
  Using “yield” for Input and Output 240
  Yielding an Iterable 242
  Recursive Algorithms Using yield* 244
Using a Generator as the Default Iterator 246
Early Termination of Generators 247
  The return() Method 247
  The throw() Method 248
Summary 249

-------------------------------------------------

next()

let arr = ['foo'];
let iter = arr[Symbol.iterator]();
console.log(iter.next()); // { done: false, value: 'foo' }
console.log(iter.next()); // { done: true, value: undefined }

-------------------------------------------------

yield*

It is possible to augment the behavior of yield to cause it to iterate through an iterable and yield its contents one at a time. This can be done using an asterisk, as shown here:

// generatorFn is equivalent to:
// function* generatorFn() {
//  for (const x of [1, 2, 3]) {
//   yield x;
//  }
// }
function* generatorFn() {
 yield* [1, 2, 3];
}