value 属性值,此时函数已经结束,多以 done 属性值为true 。
作者:互联网
第一次调用 next 方法时,从 Generator 函数的头部开始执行,先是打印了 one ,执行到 yield 就停下来,并将yield 后边表达式的值 '1',作为返回对象的 value 属性值,此时函数还没有执行完, 返回对象的 done 属性值是 false。
第二次调用 next 方法时,同上步 。
第三次调用 next 方法时,先是打印了 three ,然后执行了函数的返回操作,并将 return 后面的表达式的值,作为返回对象的 value 属性值,此时函数已经结束,多以 done 属性值为true 。
第四次调用 next 方法时, 此时函数已经执行完了,所以返回 value 属性值是 undefined ,done 属性值是 true 。如果执行第三步时,没有 return 语句的话,就直接返回 {value: undefined, done: true}。
函数返回的遍历器对象的方法
next 方法
一般情况下,next 方法不传入参数的时候,yield 表达式的返回值是 undefined 。当 next 传入参数的时候,该参数会作为上一步yield的返回值。
function* sendParameter(){
console.log("strat");
var x = yield '2';
console.log("one:" + x);
var y = yield '3';
console.log("two:" + y);
console.log("total:" + (x + y));
}
next不传参
var sendp1 = sendParameter();
sendp1.next();
// strat
// {value: "2", done: false}
sendp1.next();
// one:undefined
// {value: "3", done: false}
sendp1.next();
// two:undefined
// total:NaN
// {value: undefined, done: true}
next传参
var sendp2 = sendParameter();
sendp2.next(10);
// strat
// {value: "2", done: false}
sendp2.next(20);
// one:20
// {value: "3", done: false}
sendp2.next(30);
// two:30
// total:50
// {value: undefined, done: true}
除了使用 next ,还可以使用 for... of 循环遍历 Generator 函数生产的 Iterator 对象。
https://m.newsmth.net/article/CouponsLife/4107964
https://exp.newsmth.net/topic/1683fe33e067a9b914d6aa67e5ec0098
https://m.newsmth.net/article/CouponsLife/4107967
https://exp.newsmth.net/topic/daa314de0f9594d85766b9e0d61e4bbf
https://m.newsmth.net/article/AbroadStudy/4107981
https://exp.newsmth.net/topic/93ba4dd3f6a9471a6f717efe20d295cb
https://m.newsmth.net/article/AbroadStudy/4107984
https://exp.newsmth.net/topic/1fe8a2816f9762a24e3969c71cf60bb6
https://m.newsmth.net/article/JokeWorld/4107986
https://exp.newsmth.net/topic/e576eb8a2d42ac44370613562789f513
https://m.newsmth.net/article/JokeWorld/3926425
https://exp.newsmth.net/topic/1d65e08c8866760da671371fea0b1b09
https://m.newsmth.net/article/JokeWorld/3926429
https://exp.newsmth.net/topic/658b55de945f8183c8a3ebf9e2118ae3
https://m.newsmth.net/article/JokeWorld/3926430
https://exp.newsmth.net/topic/4e76662ae810fd6f42950fa42fb65e9d
https://m.newsmth.net/article/JokeWorld/3926432
https://exp.newsmth.net/topic/c46f583df027b9f6a24a29d9b629e4b4
https://m.newsmth.net/article/JokeWorld/3926434
https://exp.newsmth.net/topic/8dbcee542c34274bdbdb092bf00710e4
https://m.newsmth.net/article/JokeWorld/3926436
https://exp.newsmth.net/topic/f48b377a0dba52881644609bafb66591
https://m.newsmth.net/article/JokeWorld/3926438
https://exp.newsmth.net/topic/803f0f7b66b526a3f7c7cccbd923d0a9
https://m.newsmth.net/article/JokeWorld/3926442
https://exp.newsmth.net/topic/3dea5499bfc65c842f6d1c25cb00b4fd
https://m.newsmth.net/article/JokeWorld/3926447
https://exp.newsmth.net/topic/cd64b537c9bed63134ac13d1dc30dd67
https://m.newsmth.net/article/JokeWorld/3926448
https://exp.newsmth.net/topic/c959f8348505530b149a626b3e7ac387
https://m.newsmth.net/article/JokeWorld/3926449
https://exp.newsmth.net/topic/c594548719e56c4bee9111261d42c6d2
https://m.newsmth.net/article/JokeWorld/3926451
https://exp.newsmth.net/topic/6d06e1382c7fe7e2c30862f03e233fbe
https://m.newsmth.net/article/JokeWorld/3926453
https://exp.newsmth.net/topic/b2a370c16c74422c05b3be6ffbfe8178
https://m.newsmth.net/article/JokeWorld/3926454
https://exp.newsmth.net/topic/8ef1b0a626e02b588bf6e1e38ca6e6de
https://m.newsmth.net/article/JokeWorld/3926459
https://exp.newsmth.net/topic/5ed42fd3e553af67ae4c2a899de9e2ba
https://m.newsmth.net/article/Lightnovel/19298
https://exp.newsmth.net/topic/79640e4c813c37d054aaa9e8909e4da6
https://m.newsmth.net/article/Lightnovel/19305
https://exp.newsmth.net/topic/19e2763a113a99ad447c140ddd33699f
https://m.newsmth.net/article/Lightnovel/19306
https://exp.newsmth.net/topic/e6fe75fd3f76d187148af8fbdb765917
https://m.newsmth.net/article/Lightnovel/19309
https://exp.newsmth.net/topic/c23d56b4b96d4956d21a2ec597bf14a7
https://m.newsmth.net/article/Lightnovel/19311
https://exp.newsmth.net/topic/ea6766d6d133540556023b078b92e4de
https://m.newsmth.net/article/Lightnovel/19314
https://exp.newsmth.net/topic/6356592dcda5835ca447477b6db354e4
https://m.newsmth.net/article/Lightnovel/19330
https://exp.newsmth.net/topic/5a688a781c11790db0462ffe3ea20c7a
https://m.newsmth.net/article/Lightnovel/19333
https://exp.newsmth.net/topic/ecbc48559ace647a3431c234486d34de
https://m.newsmth.net/article/Lightnovel/19337
https://exp.newsmth.net/topic/f66662bb03caa3fad4adbe40ee5e63d9
https://m.newsmth.net/article/Lightnovel/19340
https://exp.newsmth.net/topic/0889cda670319a35adecff991295ef13
https://m.newsmth.net/article/Lightnovel/19346
https://exp.newsmth.net/topic/852a37c47c8248ab1c61222854f6f331
https://m.newsmth.net/article/Lightnovel/19351
https://exp.newsmth.net/topic/4d17264334e8f345f0a01c87e933da66
https://m.newsmth.net/article/Lightnovel/19352
https://exp.newsmth.net/topic/9d6d987bca3cc2a6f8d248c9cb10dec2
https://m.newsmth.net/article/Lightnovel/19357
https://exp.newsmth.net/topic/dbb0c0a1d7e72a641036bc840c2b3ccf
https://m.newsmth.net/article/Lightnovel/19359
https://exp.newsmth.net/topic/53d91fe38cfde879b18418ea39961684
https://m.newsmth.net/article/Lightnovel/19360
https://exp.newsmth.net/topic/fcb0d5b6769f95f986098b6a1d7b039e
https://m.newsmth.net/article/Lightnovel/19361
https://exp.newsmth.net/topic/d787ed4567ccb2f32f2eb7c0a6953384
https://m.newsmth.net/article/Lightnovel/19362
https://exp.newsmth.net/topic/61a5b765ff8bdd8c423882784c7213c1
https://m.newsmth.net/article/Lightnovel/
https://exp.newsmth.net/topic/
https://m.newsmth.net/article/Lightnovel/19364
https://exp.newsmth.net/topic/9a268b88782f6048ff533798af4baf37
https://m.newsmth.net/article/Lightnovel/19365
https://exp.newsmth.net/topic/81a508c81d3119b3bcd965c709243b74
https://m.newsmth.net/article/CouponsLife/4107988
https://exp.newsmth.net/topic/7eca84490362eeb95fd6617913f20cae
https://m.newsmth.net/article/CouponsLife/4107991
https://exp.newsmth.net/topic/1a3a09da39e574ac48d9344b815a9d2c
https://m.newsmth.net/article/CouponsLife/4107992
https://exp.newsmth.net/topic/9112b52546ad99ae8063d64a06ff8b69
https://m.newsmth.net/article/CouponsLife/4107993
https://exp.newsmth.net/topic/5f66b0da6b6af6e2022f4e70a9edd4e1
https://m.newsmth.net/article/CouponsLife/4107994
https://exp.newsmth.net/topic/66cbcb14a7304d27522af6f87eaf043a
https://m.newsmth.net/article/CouponsLife/4107995
https://exp.newsmth.net/topic/d22b23d316550d0eb99b62e9577a2eaf
https://m.newsmth.net/article/CouponsLife/4107997
https://exp.newsmth.net/topic/52e4d262b6f2fa82a2aa8f5c3a250300
https://m.newsmth.net/article/CouponsLife/4107998
https://exp.newsmth.net/topic/4fe76549d8451a0529855ca4973f6dac
https://m.newsmth.net/article/CouponsLife/4108001
https://exp.newsmth.net/topic/39de4129c38e31907f6a0e71f90caa45
https://m.newsmth.net/article/CouponsLife/4108004
https://exp.newsmth.net/topic/adb9c3f0867bbf9f675aa59eb99ed9bf
https://m.newsmth.net/article/CouponsLife/4108008
https://exp.newsmth.net/topic/84d0ddda84c1222581dce10bfbb1f46a
https://m.newsmth.net/article/CouponsLife/4108010
https://exp.newsmth.net/topic/8875889fa196019ef090563c4acccc57
https://m.newsmth.net/article/CouponsLife/4108011
https://exp.newsmth.net/topic/8ed3396e9302cc53ff46e1904c6b2e14
https://m.newsmth.net/article/CouponsLife/4108013
https://exp.newsmth.net/topic/b1769340a2f7b51045d085cd49f124df
https://m.newsmth.net/article/CouponsLife/4108015
https://exp.newsmth.net/topic/3d9ce3a37ed6dd7359dc11e03b2452cd
https://m.newsmth.net/article/CouponsLife/4108019
https://exp.newsmth.net/topic/c4d6f79f2342bd8db3f8ac59f018674c
https://m.newsmth.net/article/CouponsLife/4108022
https://exp.newsmth.net/topic/3c3e2165291d29ff4e311f86a7836b50
https://m.newsmth.net/article/CouponsLife/4108026
https://exp.newsmth.net/topic/c9831c98b3e3475499d44f7cf32d8c10
https://m.newsmth.net/article/CouponsLife/4108035
https://exp.newsmth.net/topic/35c61fdec8c5d4f3be54406badd806ed
https://m.newsmth.net/article/CouponsLife/
https://exp.newsmth.net/topic/
https://m.newsmth.net/article/CouponsLife/4108041
https://exp.newsmth.net/topic/06129ea24e1df7b629c22fa47ef3b885
https://m.newsmth.net/article/CouponsLife/4108043
https://exp.newsmth.net/topic/106f45ccd2eeea3c6b11b53e1f541bf7
https://m.newsmth.net/article/CouponsLife/4108044
https://exp.newsmth.net/topic/31b6f893337928653ac690bf85a98a46
https://m.newsmth.net/article/CouponsLife/4108047
https://exp.newsmth.net/topic/92cfef6e02c429cb74c7a02c28912726
https://m.newsmth.net/article/CouponsLife/4108053
https://exp.newsmth.net/topic/ae004293430c7195af2288c7c0f668a7
https://m.newsmth.net/article/CouponsLife/4108055
https://exp.newsmth.net/topic/1289d2022ed1bbf47f422f082d3aa60b
https://m.newsmth.net/article/CouponsLife/4108056
https://exp.newsmth.net/topic/e6fe4147ee330ad86e444fdd0cd3cd34
https://m.newsmth.net/article/CouponsLife/4108057
https://exp.newsmth.net/topic/ddeccdb80a4e5075f44e9ef5efbe9624
https://m.newsmth.net/article/CouponsLife/4108058
https://exp.newsmth.net/topic/c7fba30748bfad0a821373752a712bcf
https://m.newsmth.net/article/CouponsLife/4108059
https://exp.newsmth.net/topic/13181ce9985a96a491b60aa9059fb529
https://m.newsmth.net/article/CouponsLife/4108062
https://exp.newsmth.net/topic/43d74c7f89d97201967ea1efafc20946
https://m.newsmth.net/article/CouponsLife/4108064
https://exp.newsmth.net/topic/f84f96a8b4c3a5efa041f93ece9c410e
https://m.newsmth.net/article/CouponsLife/4108066
https://exp.newsmth.net/topic/ff24ad2da73be9a1d9f30810b1399a22
https://m.newsmth.net/article/CouponsLife/4108069
https://exp.newsmth.net/topic/f4c5ff1149d6e0976840550edd40b956
https://m.newsmth.net/article/CouponsLife/4108070
https://exp.newsmth.net/topic/b14081fc1ee7e551c053a5d5300d91c9
https://m.newsmth.net/article/CouponsLife/4108071
https://exp.newsmth.net/topic/09741dc8cfe0aea7837c39296c3deeb4
https://m.newsmth.net/article/CouponsLife/4108074
https://exp.newsmth.net/topic/97c355357056bab3873efc568de6d866
https://m.newsmth.net/article/CouponsLife/4108075
https://exp.newsmth.net/topic/218aeeb0fe30749d96878efe3a64c61f
https://m.newsmth.net/article/CouponsLife/4108076
https://exp.newsmth.net/topic/a1925263979c4f2ab63ef6ee2d88b7f0
标签:newsmth,exp,值为,value,topic,https,article,net,属性 来源: https://www.cnblogs.com/yoimp/p/13670798.html