码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)

时间:2019-12-29 15:17:46      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:asc   his   bcs   ble   ted   abc   cas   com   multi   

Generators allow you to use the yield * syntax to yield each iteration of nested iterable as part of the main iterations. This enables you to combine multiple arrays, strings, or any iterable with anything you want to yield from your main generator.

 

const abcs = ["A", "B", "C"]

const reverseIterator = function* (array) {
    yield* array
    yield* array.map(letter => letter.toLowerCase())
    yield Math.random()
    yield* "wan"
}

const iterator = reverseIterator(abcs)

for (let value of iterator) {
    console.log(value)
}


/*
A
B
C
a
b
c
0.1234
w
a
n
*/

 

[Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)

标签:asc   his   bcs   ble   ted   abc   cas   com   multi   

原文地址:https://www.cnblogs.com/Answer1215/p/12114849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!