Skip to content

Instantly share code, notes, and snippets.

@cymerrad
Last active April 11, 2019 17:49
Show Gist options
  • Save cymerrad/e690fbcd884c6b604d2ef0e18bf72acb to your computer and use it in GitHub Desktop.
Save cymerrad/e690fbcd884c6b604d2ef0e18bf72acb to your computer and use it in GitHub Desktop.
'use strict';
var Foo = {
*[Symbol.iterator]() {
for (let [idx, val] of this.nums.entries()) {
yield label => `${label}: ${val}
(${idx+1} / ${this.nums.length})`;
}
},
bar([x = 1, y = 2, ...nums] = []) {
this.nums = [x, y, ...nums];
}
};
Foo.bar([10,20,30,40,50]);
for (let f of Foo) {
console.log( f("Foo") );
}
/* Output:
Foo: 10
(1 / 5)
Foo: 20
(2 / 5)
Foo: 30
(3 / 5)
Foo: 40
(4 / 5)
Foo: 50
(5 / 5)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment