Last active
August 29, 2015 14:07
-
-
Save tyage/eefcc06a8e7addf25b35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// normal generator | |
function* gen() { | |
yield 2; | |
yield 3; | |
} | |
gen2 = function* () {} | |
a = gen() | |
for (b of a) { | |
console.log(b) | |
} | |
// generator method | |
obj = { | |
* a(){ | |
yield 1; | |
yield 2; | |
} | |
}; | |
a = obj.a() | |
for (b of a) { | |
console.log(b) | |
} | |
// yield can iterate | |
a = function*() { | |
var array = [1,2,3] | |
yield* array | |
yield* array | |
yield* array | |
}() | |
for (b of a) { | |
console.log(b) | |
} | |
// stop, throw (throw can be used in firefox34) | |
a = function*() { | |
var array = [1,2,3] | |
yield* array | |
yield* array | |
yield* array | |
}() | |
console.log(a.next()) | |
a.throw(new Error('stop it')) | |
console.log(a.next()) | |
// generator instance does not expose access to its generator function | |
console.log((new function *() {yield 1}).constructor) // => GeneratorFunctionPrototype |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment