Ability to specify an iterator in for...of
# Edwin Reynoso (9 years ago)
No need, just replace this
with the passed parameter:
function* keyValIterator (address) {
for (let prop in address) {
yield [ prop, address[prop] ];
}
}
var address = {
street: '420 Paper St.',
city: 'Wilmington',
state: 'Delaware'
};
for (let [ key, val ] of keyValIterator(address)) {
console.log(key, val);
}
No need, just replace `this` with the passed parameter: ```javascript function* keyValIterator (address) { for (let prop in address) { yield [ prop, address[prop] ]; } } var address = { street: '420 Paper St.', city: 'Wilmington', state: 'Delaware' }; for (let [ key, val ] of keyValIterator(address)) { console.log(key, val); } ``` On Fri, Mar 11, 2016 at 8:55 AM JD Isaacks <jd at jisaacks.com> wrote: > > I think it would be pretty useful to be able to set an iterator to use in > the context of a for...of statement. > > Consider this code: > > ```javascript > > function* keyValIterator () { > for (let prop in this) { > yield [ prop, this[prop] ]; > } > } > > var address = { > street: '420 Paper St.', > city: 'Wilmington', > state: 'Delaware' > }; > > for (let [ key, val ] of address using keyValIterator) { > console.log(key, val); > } > > ``` > > This would allow an object to be iterated without needing to assign it's > @@iterator property. It would also allow the same object to be iterated in > different ways for different needs without having to duplicate the object > and assign a different @@iterator for each one. > > The example above would only work if the iterator function specified gets > bound to the object being iterated. Not positive if that should be the case > or not. If not bound, it could easily be bound in the `for` statement using > something like `address::keyValIterator`. > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -- Thanks - Edwin -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160311/26fb8fca/attachment.html>
# Caitlin Potter (9 years ago)
I don’t know that a new syntax is really needed for this.
For the basic cases, you could just install the generator on the prototype
String.prototype.entries = function*() { yield* Array.prototype.entries.call(this.valueOf()); }
for (var entry of “str”.entries()) {
// ...
}
That bind operator proposal would allow doing a similar thing without installing anything on the prototype, as well.
I don’t know that a new syntax is really needed for this. For the basic cases, you could just install the generator on the prototype ```js String.prototype.entries = function*() { yield* Array.prototype.entries.call(this.valueOf()); } for (var entry of “str”.entries()) { // ... } ``` That bind operator proposal would allow doing a similar thing without installing anything on the prototype, as well. > On Mar 11, 2016, at 9:00 AM, Edwin Reynoso <eorroe at gmail.com> wrote: > > No need, just replace `this` with the passed parameter: > > ```javascript > function* keyValIterator (address) { > for (let prop in address) { > yield [ prop, address[prop] ]; > } > } > > var address = { > street: '420 Paper St.', > city: 'Wilmington', > state: 'Delaware' > }; > > for (let [ key, val ] of keyValIterator(address)) { > console.log(key, val); > } > ``` > > On Fri, Mar 11, 2016 at 8:55 AM JD Isaacks <jd at jisaacks.com <mailto:jd at jisaacks.com>> wrote: > > I think it would be pretty useful to be able to set an iterator to use in the context of a for...of statement. > > Consider this code: > > ```javascript > function* keyValIterator () { > for (let prop in this) { > yield [ prop, this[prop] ]; > } > } > > var address = { > street: '420 Paper St.', > city: 'Wilmington', > state: 'Delaware' > }; > > for (let [ key, val ] of address using keyValIterator) { > console.log(key, val); > } > ``` > > This would allow an object to be iterated without needing to assign it's @@iterator property. It would also allow the same object to be iterated in different ways for different needs without having to duplicate the object and assign a different @@iterator for each one. > > The example above would only work if the iterator function specified gets bound to the object being iterated. Not positive if that should be the case or not. If not bound, it could easily be bound in the `for` statement using something like `address::keyValIterator`. > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org <mailto:es-discuss at mozilla.org> > https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss> > -- > Thanks > - Edwin > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160311/98c6a524/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 842 bytes Desc: Message signed with OpenPGP using GPGMail URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160311/98c6a524/attachment.sig>
# JD Isaacks (9 years ago)
No need, just replace
this
with the passed parameter:
Edwin, ahh, that is great! I had not considered that, thank you!
> No need, just replace `this` with the passed parameter: Edwin, ahh, that is great! I had not considered that, thank you! On Fri, Mar 11, 2016 at 9:00 AM, Edwin Reynoso <eorroe at gmail.com> wrote: > No need, just replace `this` with the passed parameter: > > ```javascript > function* keyValIterator (address) { for (let prop in address) { yield [ > prop, address[prop] ]; } } var address = { street: '420 Paper St.', city: > 'Wilmington', state: 'Delaware' }; for (let [ key, val ] of > keyValIterator(address)) { console.log(key, val); } > ``` > > On Fri, Mar 11, 2016 at 8:55 AM JD Isaacks <jd at jisaacks.com> wrote: > >> >> I think it would be pretty useful to be able to set an iterator to use in >> the context of a for...of statement. >> >> Consider this code: >> >> ```javascript >> >> function* keyValIterator () { >> for (let prop in this) { >> yield [ prop, this[prop] ]; >> } >> } >> >> var address = { >> street: '420 Paper St.', >> city: 'Wilmington', >> state: 'Delaware' >> }; >> >> for (let [ key, val ] of address using keyValIterator) { >> console.log(key, val); >> } >> >> ``` >> >> This would allow an object to be iterated without needing to assign it's >> @@iterator property. It would also allow the same object to be iterated in >> different ways for different needs without having to duplicate the object >> and assign a different @@iterator for each one. >> >> The example above would only work if the iterator function specified gets >> bound to the object being iterated. Not positive if that should be the case >> or not. If not bound, it could easily be bound in the `for` statement using >> something like `address::keyValIterator`. >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > -- > Thanks > - Edwin > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160311/127e6351/attachment-0001.html>
I think it would be pretty useful to be able to set an iterator to use in the context of a for...of statement.
Consider this code:
function* keyValIterator () { for (let prop in this) { yield [ prop, this[prop] ]; } } var address = { street: '420 Paper St.', city: 'Wilmington', state: 'Delaware' }; for (let [ key, val ] of address using keyValIterator) { console.log(key, val); }
This would allow an object to be iterated without needing to assign it's @@iterator property. It would also allow the same object to be iterated in different ways for different needs without having to duplicate the object and assign a different @@iterator for each one.
The example above would only work if the iterator function specified gets bound to the object being iterated. Not positive if that should be the case or not. If not bound, it could easily be bound in the
for
statement using something likeaddress::keyValIterator
.