js0019 发表于 2018-12-15 | 阅读次数: 字数统计: 162 | 阅读时长 ≈ 1 作为生成器函数参数发送值 12345678910111213141516171819202122232425262728293031<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>Sending data to and recieving data from a generator</title> <link rel="stylesheet" href="../assert.css"> <script src="../assert.js"></script> </head> <body> <script> function* NinjaGenerator(action) { const imposter = yield ("Hatori " + action); assert(imposter === "Hanzo", "The generator has been infiltrated"); yield ("Yoshi (" + imposter + ") " + action); } const ninjaIterator = NinjaGenerator("skulk"); const result1 = ninjaIterator.next(); assert(result1.value === "Hatori skulk", "Hatori is skulking"); const result2 = ninjaIterator.next("Hanzo"); assert(result2.value === "Yoshi (Hanzo) skulk", "We have an imposter!"); </script> </body></html>