Object.prototype.forIn
You can already achieve this in ES5 with
Object.keys(obj).forEach(key => {
});
or in ES6 with
var key;
for (key of Object.keys(obj)){
}
And with the new proposal, you'll also have Object.values
for values and
Object.entries
for key/value pairs:
tc39/proposal
You can already achieve this in ES5 with ``` Object.keys(obj).forEach(key => { }); ``` or in ES6 with ``` var key; for (key of Object.keys(obj)){ } ``` And with the new proposal, you'll also have `Object.values` for values and `Object.entries` for key/value pairs: https://github.com/tc39/proposal-object-values-entries On Fri, Mar 4, 2016 at 8:22 AM, Langdon <langdon at gmail.com> wrote: > My apologies if this has been discussed before (I have to imagine it has, > but couldn't find anything). > > Why isn't there a `forIn` method on Object natively? > > Something that simply wraps this all-to-common code: > > > var key; > > for (key in obj) { > if (obj.hasOwnProperty(key) === true) { > ... > } > } > > Example: https://jsfiddle.net/langdonx/d4Lph13u/ > > TIA, > Langdon > > _______________________________________________ > 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/20160304/bf8621c6/attachment-0001.html>
Ah, thanks. I had forgotten about Object.keys, but even that still doesn't
wrap things up as nicely as forIn
.
Object.entries looks nice.
Ah, thanks. I had forgotten about Object.keys, but even that still doesn't wrap things up as nicely as `forIn`. Object.entries looks nice. On Fri, Mar 4, 2016 at 11:37 AM, Logan Smyth <loganfsmyth at gmail.com> wrote: > You can already achieve this in ES5 with > > ``` > Object.keys(obj).forEach(key => { > > }); > ``` > > or in ES6 with > > ``` > var key; > for (key of Object.keys(obj)){ > > } > ``` > > And with the new proposal, you'll also have `Object.values` for values and > `Object.entries` for key/value pairs: > https://github.com/tc39/proposal-object-values-entries > > On Fri, Mar 4, 2016 at 8:22 AM, Langdon <langdon at gmail.com> wrote: > >> My apologies if this has been discussed before (I have to imagine it has, >> but couldn't find anything). >> >> Why isn't there a `forIn` method on Object natively? >> >> Something that simply wraps this all-to-common code: >> >> >> var key; >> >> for (key in obj) { >> if (obj.hasOwnProperty(key) === true) { >> ... >> } >> } >> >> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >> >> TIA, >> Langdon >> >> _______________________________________________ >> 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/20160304/fe45b21f/attachment.html>
Not sure of the rationale; however, it looks like Chrome now supports something similar natively:
twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email, twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email
From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Langdon Sent: Friday, March 4, 2016 11:22 AM To: es-discuss at mozilla.org Subject: Object.prototype.forIn
My apologies if this has been discussed before (I have to imagine it has, but couldn't find anything).
Why isn't there a forIn
method on Object natively?
Something that simply wraps this all-to-common code:
var key;
for (key in obj) {
if (obj.hasOwnProperty(key) === true) {
...
}
}
Example: jsfiddle.net/langdonx/d4Lph13u
TIA,
Langdon
Not sure of the rationale; however, it looks like Chrome now supports something similar natively: <https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Langdon Sent: Friday, March 4, 2016 11:22 AM To: es-discuss at mozilla.org Subject: Object.prototype.forIn My apologies if this has been discussed before (I have to imagine it has, but couldn't find anything). Why isn't there a `forIn` method on Object natively? Something that simply wraps this all-to-common code: var key; for (key in obj) { if (obj.hasOwnProperty(key) === true) { ... } } Example: https://jsfiddle.net/langdonx/d4Lph13u/ TIA, Langdon -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160304/e8ad8034/attachment-0001.html>
for (let key of Object.keys(obj)) { ... }
for (let key of Object.keys(obj)) { ... } On Fri, Mar 4, 2016 at 4:48 PM, Langdon <langdon at gmail.com> wrote: > Ah, thanks. I had forgotten about Object.keys, but even that still > doesn't wrap things up as nicely as `forIn`. > > Object.entries looks nice. > > On Fri, Mar 4, 2016 at 11:37 AM, Logan Smyth <loganfsmyth at gmail.com> > wrote: > >> You can already achieve this in ES5 with >> >> ``` >> Object.keys(obj).forEach(key => { >> >> }); >> ``` >> >> or in ES6 with >> >> ``` >> var key; >> for (key of Object.keys(obj)){ >> >> } >> ``` >> >> And with the new proposal, you'll also have `Object.values` for values >> and `Object.entries` for key/value pairs: >> https://github.com/tc39/proposal-object-values-entries >> >> On Fri, Mar 4, 2016 at 8:22 AM, Langdon <langdon at gmail.com> wrote: >> >>> My apologies if this has been discussed before (I have to imagine it >>> has, but couldn't find anything). >>> >>> Why isn't there a `forIn` method on Object natively? >>> >>> Something that simply wraps this all-to-common code: >>> >>> >>> var key; >>> >>> for (key in obj) { >>> if (obj.hasOwnProperty(key) === true) { >>> ... >>> } >>> } >>> >>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>> >>> TIA, >>> Langdon >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> > > _______________________________________________ > 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/20160304/4ffa2a97/attachment.html>
Yeah, and those effectively nullify this, anyways.
Yeah, and those effectively nullify this, anyways. On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com> wrote: > Not sure of the rationale; however, it looks like Chrome now supports > something similar natively: > > > > > https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email > > > > *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On Behalf Of * > Langdon > *Sent:* Friday, March 4, 2016 11:22 AM > *To:* es-discuss at mozilla.org > *Subject:* Object.prototype.forIn > > > > My apologies if this has been discussed before (I have to imagine it has, > but couldn't find anything). > > > > Why isn't there a `forIn` method on Object natively? > > > > Something that simply wraps this all-to-common code: > > > > var key; > > > > for (key in obj) { > > if (obj.hasOwnProperty(key) === true) { > > ... > > } > > } > > > > Example: https://jsfiddle.net/langdonx/d4Lph13u/ > > > > TIA, > > Langdon > _______________________________________________ > 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/20160304/9b06b16e/attachment.html>
My gripe with Object.keys is that it requires a closure to use effectively.
Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair.
As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be.
My gripe with Object.keys is that it requires a closure to use effectively. Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair. As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be. On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com> wrote: > Yeah, and those effectively nullify this, anyways. > > On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com> > wrote: > >> Not sure of the rationale; however, it looks like Chrome now supports >> something similar natively: >> >> >> >> >> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >> >> >> >> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On Behalf Of >> *Langdon >> *Sent:* Friday, March 4, 2016 11:22 AM >> *To:* es-discuss at mozilla.org >> *Subject:* Object.prototype.forIn >> >> >> >> My apologies if this has been discussed before (I have to imagine it has, >> but couldn't find anything). >> >> >> >> Why isn't there a `forIn` method on Object natively? >> >> >> >> Something that simply wraps this all-to-common code: >> >> >> >> var key; >> >> >> >> for (key in obj) { >> >> if (obj.hasOwnProperty(key) === true) { >> >> ... >> >> } >> >> } >> >> >> >> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >> >> >> >> TIA, >> >> Langdon >> _______________________________________________ >> 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/20160304/c8693d25/attachment-0001.html>
No doubt … which brings us right back to the rationale question!
From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Langdon Sent: Friday, March 4, 2016 1:28 PM To: es-discuss at mozilla.org Subject: Re: Object.prototype.forIn
My gripe with Object.keys is that it requires a closure to use effectively.
Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair.
As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be.
On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com <mailto:isiahmeadows at gmail.com> > wrote:
Yeah, and those effectively nullify this, anyways.
On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com <mailto:syblackwell at anywhichway.com> > wrote:
Not sure of the rationale; however, it looks like Chrome now supports something similar natively:
twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email, twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email
From: es-discuss [mailto:es-discuss-bounces at mozilla.org <mailto:es-discuss-bounces at mozilla.org> ] On Behalf Of Langdon
Sent: Friday, March 4, 2016 11:22 AM To: es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
Subject: Object.prototype.forIn
My apologies if this has been discussed before (I have to imagine it has, but couldn't find anything).
Why isn't there a forIn
method on Object natively?
Something that simply wraps this all-to-common code:
var key;
for (key in obj) {
if (obj.hasOwnProperty(key) === true) {
...
}
}
Example: jsfiddle.net/langdonx/d4Lph13u
TIA,
Langdon
No doubt … which brings us right back to the rationale question! From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Langdon Sent: Friday, March 4, 2016 1:28 PM To: es-discuss at mozilla.org Subject: Re: Object.prototype.forIn My gripe with Object.keys is that it requires a closure to use effectively. Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair. As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be. On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com <mailto:isiahmeadows at gmail.com> > wrote: Yeah, and those effectively nullify this, anyways. On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com <mailto:syblackwell at anywhichway.com> > wrote: Not sure of the rationale; however, it looks like Chrome now supports something similar natively: <https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email From: es-discuss [mailto:es-discuss-bounces at mozilla.org <mailto:es-discuss-bounces at mozilla.org> ] On Behalf Of Langdon Sent: Friday, March 4, 2016 11:22 AM To: es-discuss at mozilla.org <mailto:es-discuss at mozilla.org> Subject: Object.prototype.forIn My apologies if this has been discussed before (I have to imagine it has, but couldn't find anything). Why isn't there a `forIn` method on Object natively? Something that simply wraps this all-to-common code: var key; for (key in obj) { if (obj.hasOwnProperty(key) === true) { ... } } Example: https://jsfiddle.net/langdonx/d4Lph13u/ TIA, Langdon _______________________________________________ es-discuss mailing list es-discuss at mozilla.org <mailto: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/20160304/5b3879df/attachment.html>
Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair.
What’s the problem with for (let [key, value] of Object.entries(obj)) { …. }
As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be.
You might be surprised — If the pair never reaches geriatric status (old generation/tenured space, etc), it could be allocated and cleaned up relatively quickly.
> Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair. What’s the problem with `for (let [key, value] of Object.entries(obj)) { …. }` > As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be. You might be surprised — If the pair never reaches geriatric status (old generation/tenured space, etc), it could be allocated and cleaned up relatively quickly. > On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: > > My gripe with Object.keys is that it requires a closure to use effectively. > > Object.entries does look nice, but 2 arguments is more straightforward than a passing around a pair. > > As well (and perhaps more importantly), temporarily building an array of arrays so we can forEach it, seems way less efficient than forIn is/would be. > > On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com <mailto:isiahmeadows at gmail.com>> wrote: > Yeah, and those effectively nullify this, anyways. > > On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com <mailto:syblackwell at anywhichway.com>> wrote: > Not sure of the rationale; however, it looks like Chrome now supports something similar natively: > > > > <>https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email <https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email> > > > From: es-discuss [mailto:es-discuss-bounces at mozilla.org <mailto:es-discuss-bounces at mozilla.org>] On Behalf Of Langdon > Sent: Friday, March 4, 2016 11:22 AM > To: es-discuss at mozilla.org <mailto:es-discuss at mozilla.org> > Subject: Object.prototype.forIn > > > > My apologies if this has been discussed before (I have to imagine it has, but couldn't find anything). > > > > Why isn't there a `forIn` method on Object natively? > > > > Something that simply wraps this all-to-common code: > > > > var key; > > > > for (key in obj) { > > if (obj.hasOwnProperty(key) === true) { > > ... > > } > > } > > > > Example: https://jsfiddle.net/langdonx/d4Lph13u/ <https://jsfiddle.net/langdonx/d4Lph13u/> > > > TIA, > > Langdon > > _______________________________________________ > 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> > > _______________________________________________ > 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/20160304/23b612a8/attachment-0001.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/20160304/23b612a8/attachment-0001.sig>
Ahhh, nothing. I never think about destructuring.
Ahhh, nothing. I never think about destructuring. Thanks! On Fri, Mar 4, 2016 at 1:41 PM, Caitlin Potter <caitpotter88 at gmail.com> wrote: > > Object.entries does look nice, but 2 arguments is more straightforward > than a passing around a pair. > > What’s the problem with `for (let [key, value] of Object.entries(obj)) { > …. }` > > > As well (and perhaps more importantly), temporarily building an array of > arrays so we can forEach it, seems way less efficient than forIn is/would > be. > > You might be surprised — If the pair never reaches geriatric status (old > generation/tenured space, etc), it could be allocated and cleaned up > relatively quickly. > > On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: > > My gripe with Object.keys is that it requires a closure to use effectively. > > Object.entries does look nice, but 2 arguments is more straightforward > than a passing around a pair. > > As well (and perhaps more importantly), temporarily building an array of > arrays so we can forEach it, seems way less efficient than forIn is/would > be. > > On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com> > wrote: > >> Yeah, and those effectively nullify this, anyways. >> >> On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com> >> wrote: >> >>> Not sure of the rationale; however, it looks like Chrome now supports >>> something similar natively: >>> >>> >>> >>> >>> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >>> >>> >>> >>> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On Behalf >>> Of *Langdon >>> *Sent:* Friday, March 4, 2016 11:22 AM >>> *To:* es-discuss at mozilla.org >>> *Subject:* Object.prototype.forIn >>> >>> >>> >>> My apologies if this has been discussed before (I have to imagine it >>> has, but couldn't find anything). >>> >>> >>> >>> Why isn't there a `forIn` method on Object natively? >>> >>> >>> >>> Something that simply wraps this all-to-common code: >>> >>> >>> >>> var key; >>> >>> >>> >>> for (key in obj) { >>> >>> if (obj.hasOwnProperty(key) === true) { >>> >>> ... >>> >>> } >>> >>> } >>> >>> >>> >>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>> >>> >>> >>> TIA, >>> >>> Langdon >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> > _______________________________________________ > 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/20160304/97fe793b/attachment.html>
Sorry guys but this is very wrong, for in, loops through all properties even the ones inherited from all prototypes, while Object.keys() and Object.entries() do not. They are indeed very different
Sorry guys but this is very wrong, for in, loops through all properties even the ones inherited from all prototypes, while Object.keys() and Object.entries() do not. They are indeed very different On Fri, Mar 4, 2016 at 1:45 PM Langdon <langdon at gmail.com> wrote: > Ahhh, nothing. I never think about destructuring. Thanks! > > On Fri, Mar 4, 2016 at 1:41 PM, Caitlin Potter <caitpotter88 at gmail.com> > wrote: > >> > Object.entries does look nice, but 2 arguments is more straightforward >> than a passing around a pair. >> >> What’s the problem with `for (let [key, value] of Object.entries(obj)) { >> …. }` >> >> > As well (and perhaps more importantly), temporarily building an array >> of arrays so we can forEach it, seems way less efficient than forIn >> is/would be. >> >> You might be surprised — If the pair never reaches geriatric status (old >> generation/tenured space, etc), it could be allocated and cleaned up >> relatively quickly. >> >> On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: >> >> My gripe with Object.keys is that it requires a closure to use >> effectively. >> >> Object.entries does look nice, but 2 arguments is more straightforward >> than a passing around a pair. >> >> As well (and perhaps more importantly), temporarily building an array of >> arrays so we can forEach it, seems way less efficient than forIn is/would >> be. >> >> On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com> >> wrote: >> >>> Yeah, and those effectively nullify this, anyways. >>> >>> On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com> >>> wrote: >>> >>>> Not sure of the rationale; however, it looks like Chrome now supports >>>> something similar natively: >>>> >>>> >>>> >>>> >>>> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >>>> >>>> >>>> >>>> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On Behalf >>>> Of *Langdon >>>> *Sent:* Friday, March 4, 2016 11:22 AM >>>> *To:* es-discuss at mozilla.org >>>> *Subject:* Object.prototype.forIn >>>> >>>> >>>> >>>> My apologies if this has been discussed before (I have to imagine it >>>> has, but couldn't find anything). >>>> >>>> >>>> >>>> Why isn't there a `forIn` method on Object natively? >>>> >>>> >>>> >>>> Something that simply wraps this all-to-common code: >>>> >>>> >>>> >>>> var key; >>>> >>>> >>>> >>>> for (key in obj) { >>>> >>>> if (obj.hasOwnProperty(key) === true) { >>>> >>>> ... >>>> >>>> } >>>> >>>> } >>>> >>>> >>>> >>>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>>> >>>> >>>> >>>> TIA, >>>> >>>> Langdon >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> >> > _______________________________________________ > 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/20160304/da0ba512/attachment-0001.html>
Edwin, the original example loop explicitly checks obj.hasOwnProperty(key)
,
so properties in the prototype chain are not an issue here.
Edwin, the original example loop explicitly checks `obj.hasOwnProperty(key)`, so properties in the prototype chain are not an issue here. On Fri, Mar 4, 2016 at 1:04 PM, Edwin Reynoso <eorroe at gmail.com> wrote: > Sorry guys but this is very wrong, for in, loops through all properties > even the ones inherited from all prototypes, while Object.keys() and > Object.entries() do not. They are indeed very different > > On Fri, Mar 4, 2016 at 1:45 PM Langdon <langdon at gmail.com> wrote: > >> Ahhh, nothing. I never think about destructuring. Thanks! >> >> On Fri, Mar 4, 2016 at 1:41 PM, Caitlin Potter <caitpotter88 at gmail.com> >> wrote: >> >>> > Object.entries does look nice, but 2 arguments is more straightforward >>> than a passing around a pair. >>> >>> What’s the problem with `for (let [key, value] of Object.entries(obj)) { >>> …. }` >>> >>> > As well (and perhaps more importantly), temporarily building an array >>> of arrays so we can forEach it, seems way less efficient than forIn >>> is/would be. >>> >>> You might be surprised — If the pair never reaches geriatric status (old >>> generation/tenured space, etc), it could be allocated and cleaned up >>> relatively quickly. >>> >>> On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: >>> >>> My gripe with Object.keys is that it requires a closure to use >>> effectively. >>> >>> Object.entries does look nice, but 2 arguments is more straightforward >>> than a passing around a pair. >>> >>> As well (and perhaps more importantly), temporarily building an array of >>> arrays so we can forEach it, seems way less efficient than forIn is/would >>> be. >>> >>> On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com> >>> wrote: >>> >>>> Yeah, and those effectively nullify this, anyways. >>>> >>>> On Fri, Mar 4, 2016, 12:55 Simon Blackwell <syblackwell at anywhichway.com> >>>> wrote: >>>> >>>>> Not sure of the rationale; however, it looks like Chrome now supports >>>>> something similar natively: >>>>> >>>>> >>>>> >>>>> >>>>> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >>>>> >>>>> >>>>> >>>>> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On Behalf >>>>> Of *Langdon >>>>> *Sent:* Friday, March 4, 2016 11:22 AM >>>>> *To:* es-discuss at mozilla.org >>>>> *Subject:* Object.prototype.forIn >>>>> >>>>> >>>>> >>>>> My apologies if this has been discussed before (I have to imagine it >>>>> has, but couldn't find anything). >>>>> >>>>> >>>>> >>>>> Why isn't there a `forIn` method on Object natively? >>>>> >>>>> >>>>> >>>>> Something that simply wraps this all-to-common code: >>>>> >>>>> >>>>> >>>>> var key; >>>>> >>>>> >>>>> >>>>> for (key in obj) { >>>>> >>>>> if (obj.hasOwnProperty(key) === true) { >>>>> >>>>> ... >>>>> >>>>> } >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>>>> >>>>> >>>>> >>>>> TIA, >>>>> >>>>> Langdon >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> 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/20160304/e36865f0/attachment.html>
Okay that makes sense, but the Object.forIn
confused me, then that'd be a
bad name because then its not really looping like for
Okay that makes sense, but the `Object.forIn` confused me, then that'd be a bad name because then its not really looping like for-in On Fri, Mar 4, 2016 at 4:07 PM Logan Smyth <loganfsmyth at gmail.com> wrote: > Edwin, the original example loop explicitly checks `obj.hasOwnProperty(key)`, > so properties in the prototype chain are not an issue here. > > On Fri, Mar 4, 2016 at 1:04 PM, Edwin Reynoso <eorroe at gmail.com> wrote: > >> Sorry guys but this is very wrong, for in, loops through all properties >> even the ones inherited from all prototypes, while Object.keys() and >> Object.entries() do not. They are indeed very different >> >> On Fri, Mar 4, 2016 at 1:45 PM Langdon <langdon at gmail.com> wrote: >> >>> Ahhh, nothing. I never think about destructuring. Thanks! >>> >>> On Fri, Mar 4, 2016 at 1:41 PM, Caitlin Potter <caitpotter88 at gmail.com> >>> wrote: >>> >>>> > Object.entries does look nice, but 2 arguments is more >>>> straightforward than a passing around a pair. >>>> >>>> What’s the problem with `for (let [key, value] of Object.entries(obj)) >>>> { …. }` >>>> >>>> > As well (and perhaps more importantly), temporarily building an array >>>> of arrays so we can forEach it, seems way less efficient than forIn >>>> is/would be. >>>> >>>> You might be surprised — If the pair never reaches geriatric status >>>> (old generation/tenured space, etc), it could be allocated and cleaned up >>>> relatively quickly. >>>> >>>> On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: >>>> >>>> My gripe with Object.keys is that it requires a closure to use >>>> effectively. >>>> >>>> Object.entries does look nice, but 2 arguments is more straightforward >>>> than a passing around a pair. >>>> >>>> As well (and perhaps more importantly), temporarily building an array >>>> of arrays so we can forEach it, seems way less efficient than forIn >>>> is/would be. >>>> >>>> On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com> >>>> wrote: >>>> >>>>> Yeah, and those effectively nullify this, anyways. >>>>> >>>>> On Fri, Mar 4, 2016, 12:55 Simon Blackwell < >>>>> syblackwell at anywhichway.com> wrote: >>>>> >>>>>> Not sure of the rationale; however, it looks like Chrome now supports >>>>>> something similar natively: >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >>>>>> >>>>>> >>>>>> >>>>>> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On >>>>>> Behalf Of *Langdon >>>>>> *Sent:* Friday, March 4, 2016 11:22 AM >>>>>> *To:* es-discuss at mozilla.org >>>>>> *Subject:* Object.prototype.forIn >>>>>> >>>>>> >>>>>> >>>>>> My apologies if this has been discussed before (I have to imagine it >>>>>> has, but couldn't find anything). >>>>>> >>>>>> >>>>>> >>>>>> Why isn't there a `forIn` method on Object natively? >>>>>> >>>>>> >>>>>> >>>>>> Something that simply wraps this all-to-common code: >>>>>> >>>>>> >>>>>> >>>>>> var key; >>>>>> >>>>>> >>>>>> >>>>>> for (key in obj) { >>>>>> >>>>>> if (obj.hasOwnProperty(key) === true) { >>>>>> >>>>>> ... >>>>>> >>>>>> } >>>>>> >>>>>> } >>>>>> >>>>>> >>>>>> >>>>>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>>>>> >>>>>> >>>>>> >>>>>> TIA, >>>>>> >>>>>> Langdon >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> 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 >> >> > -- Thanks - Edwin -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160304/d39d0578/attachment-0001.html>
While I would like a function for this too, this is pretty trivial:
function* enumerate(obj) { for (var key in obj) { yield [key, obj[key]]; } }
for (const [key, value] of enumerate(obj) {
doStuff(key, value);
}
While I would like a function for this too, this is pretty trivial: ``` function* enumerate(obj) { for (var key in obj) { yield [key, obj[key]]; } } for (const [key, value] of enumerate(obj) { doStuff(key, value); } ``` On Fri, Mar 4, 2016 at 1:08 PM, Edwin Reynoso <eorroe at gmail.com> wrote: > Okay that makes sense, but the `Object.forIn` confused me, then that'd be > a bad name because then its not really looping like for-in > > On Fri, Mar 4, 2016 at 4:07 PM Logan Smyth <loganfsmyth at gmail.com> wrote: > >> Edwin, the original example loop explicitly checks `obj.hasOwnProperty(key)`, >> so properties in the prototype chain are not an issue here. >> >> On Fri, Mar 4, 2016 at 1:04 PM, Edwin Reynoso <eorroe at gmail.com> wrote: >> >>> Sorry guys but this is very wrong, for in, loops through all properties >>> even the ones inherited from all prototypes, while Object.keys() and >>> Object.entries() do not. They are indeed very different >>> >>> On Fri, Mar 4, 2016 at 1:45 PM Langdon <langdon at gmail.com> wrote: >>> >>>> Ahhh, nothing. I never think about destructuring. Thanks! >>>> >>>> On Fri, Mar 4, 2016 at 1:41 PM, Caitlin Potter <caitpotter88 at gmail.com> >>>> wrote: >>>> >>>>> > Object.entries does look nice, but 2 arguments is more >>>>> straightforward than a passing around a pair. >>>>> >>>>> What’s the problem with `for (let [key, value] of Object.entries(obj)) >>>>> { …. }` >>>>> >>>>> > As well (and perhaps more importantly), temporarily building an >>>>> array of arrays so we can forEach it, seems way less efficient than forIn >>>>> is/would be. >>>>> >>>>> You might be surprised — If the pair never reaches geriatric status >>>>> (old generation/tenured space, etc), it could be allocated and cleaned up >>>>> relatively quickly. >>>>> >>>>> On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: >>>>> >>>>> My gripe with Object.keys is that it requires a closure to use >>>>> effectively. >>>>> >>>>> Object.entries does look nice, but 2 arguments is more straightforward >>>>> than a passing around a pair. >>>>> >>>>> As well (and perhaps more importantly), temporarily building an array >>>>> of arrays so we can forEach it, seems way less efficient than forIn >>>>> is/would be. >>>>> >>>>> On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com> >>>>> wrote: >>>>> >>>>>> Yeah, and those effectively nullify this, anyways. >>>>>> >>>>>> On Fri, Mar 4, 2016, 12:55 Simon Blackwell < >>>>>> syblackwell at anywhichway.com> wrote: >>>>>> >>>>>>> Not sure of the rationale; however, it looks like Chrome now >>>>>>> supports something similar natively: >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>>> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >>>>>>> >>>>>>> >>>>>>> >>>>>>> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On >>>>>>> Behalf Of *Langdon >>>>>>> *Sent:* Friday, March 4, 2016 11:22 AM >>>>>>> *To:* es-discuss at mozilla.org >>>>>>> *Subject:* Object.prototype.forIn >>>>>>> >>>>>>> >>>>>>> >>>>>>> My apologies if this has been discussed before (I have to imagine it >>>>>>> has, but couldn't find anything). >>>>>>> >>>>>>> >>>>>>> >>>>>>> Why isn't there a `forIn` method on Object natively? >>>>>>> >>>>>>> >>>>>>> >>>>>>> Something that simply wraps this all-to-common code: >>>>>>> >>>>>>> >>>>>>> >>>>>>> var key; >>>>>>> >>>>>>> >>>>>>> >>>>>>> for (key in obj) { >>>>>>> >>>>>>> if (obj.hasOwnProperty(key) === true) { >>>>>>> >>>>>>> ... >>>>>>> >>>>>>> } >>>>>>> >>>>>>> } >>>>>>> >>>>>>> >>>>>>> >>>>>>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>>>>>> >>>>>>> >>>>>>> >>>>>>> TIA, >>>>>>> >>>>>>> Langdon >>>>>>> _______________________________________________ >>>>>>> es-discuss mailing list >>>>>>> es-discuss at mozilla.org >>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>> >>>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> 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 >>> >>> >> -- > 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/20160304/25906f61/attachment.html>
Nice use of generators! I'll save this one into my toolbox.
/#!/JoePea
Nice use of generators! I'll save this one into my toolbox. /#!/JoePea On Mar 4, 2016 6:43 PM, "Jordan Harband" <ljharb at gmail.com> wrote: > While I would like a function for this too, this is pretty trivial: > > ``` > function* enumerate(obj) { for (var key in obj) { yield [key, obj[key]]; } > } > > for (const [key, value] of enumerate(obj) { > doStuff(key, value); > } > ``` > > On Fri, Mar 4, 2016 at 1:08 PM, Edwin Reynoso <eorroe at gmail.com> wrote: > >> Okay that makes sense, but the `Object.forIn` confused me, then that'd be >> a bad name because then its not really looping like for-in >> >> On Fri, Mar 4, 2016 at 4:07 PM Logan Smyth <loganfsmyth at gmail.com> wrote: >> >>> Edwin, the original example loop explicitly checks `obj.hasOwnProperty(key)`, >>> so properties in the prototype chain are not an issue here. >>> >>> On Fri, Mar 4, 2016 at 1:04 PM, Edwin Reynoso <eorroe at gmail.com> wrote: >>> >>>> Sorry guys but this is very wrong, for in, loops through all properties >>>> even the ones inherited from all prototypes, while Object.keys() and >>>> Object.entries() do not. They are indeed very different >>>> >>>> On Fri, Mar 4, 2016 at 1:45 PM Langdon <langdon at gmail.com> wrote: >>>> >>>>> Ahhh, nothing. I never think about destructuring. Thanks! >>>>> >>>>> On Fri, Mar 4, 2016 at 1:41 PM, Caitlin Potter <caitpotter88 at gmail.com >>>>> > wrote: >>>>> >>>>>> > Object.entries does look nice, but 2 arguments is more >>>>>> straightforward than a passing around a pair. >>>>>> >>>>>> What’s the problem with `for (let [key, value] of >>>>>> Object.entries(obj)) { …. }` >>>>>> >>>>>> > As well (and perhaps more importantly), temporarily building an >>>>>> array of arrays so we can forEach it, seems way less efficient than forIn >>>>>> is/would be. >>>>>> >>>>>> You might be surprised — If the pair never reaches geriatric status >>>>>> (old generation/tenured space, etc), it could be allocated and cleaned up >>>>>> relatively quickly. >>>>>> >>>>>> On Mar 4, 2016, at 1:28 PM, Langdon <langdon at gmail.com> wrote: >>>>>> >>>>>> My gripe with Object.keys is that it requires a closure to use >>>>>> effectively. >>>>>> >>>>>> Object.entries does look nice, but 2 arguments is more >>>>>> straightforward than a passing around a pair. >>>>>> >>>>>> As well (and perhaps more importantly), temporarily building an array >>>>>> of arrays so we can forEach it, seems way less efficient than forIn >>>>>> is/would be. >>>>>> >>>>>> On Fri, Mar 4, 2016 at 1:20 PM, Isiah Meadows <isiahmeadows at gmail.com >>>>>> > wrote: >>>>>> >>>>>>> Yeah, and those effectively nullify this, anyways. >>>>>>> >>>>>>> On Fri, Mar 4, 2016, 12:55 Simon Blackwell < >>>>>>> syblackwell at anywhichway.com> wrote: >>>>>>> >>>>>>>> Not sure of the rationale; however, it looks like Chrome now >>>>>>>> supports something similar natively: >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> https://twitter.com/malyw/status/704972953029623808?utm_source=javascriptweekly&utm_medium=email >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On >>>>>>>> Behalf Of *Langdon >>>>>>>> *Sent:* Friday, March 4, 2016 11:22 AM >>>>>>>> *To:* es-discuss at mozilla.org >>>>>>>> *Subject:* Object.prototype.forIn >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> My apologies if this has been discussed before (I have to imagine >>>>>>>> it has, but couldn't find anything). >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> Why isn't there a `forIn` method on Object natively? >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> Something that simply wraps this all-to-common code: >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> var key; >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> for (key in obj) { >>>>>>>> >>>>>>>> if (obj.hasOwnProperty(key) === true) { >>>>>>>> >>>>>>>> ... >>>>>>>> >>>>>>>> } >>>>>>>> >>>>>>>> } >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> Example: https://jsfiddle.net/langdonx/d4Lph13u/ >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> TIA, >>>>>>>> >>>>>>>> Langdon >>>>>>>> _______________________________________________ >>>>>>>> es-discuss mailing list >>>>>>>> es-discuss at mozilla.org >>>>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>>>> >>>>>>> >>>>>> _______________________________________________ >>>>>> es-discuss mailing list >>>>>> es-discuss at mozilla.org >>>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>>> >>>>>> >>>>>> >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> 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 >>>> >>>> >>> -- >> Thanks >> - Edwin >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > > _______________________________________________ > 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/20160305/2526ce85/attachment-0001.html>
Langdon wrote:
Something that simply wraps this all-to-common code:
var key;
for (key in obj) { if (obj.hasOwnProperty(key) === true) { ... } }
Imo the problem is not that there is no wrapper method for this pattern, but that this code is all too common. It should rarely be needed, and is mostly wrong anyway.
The cases to consider are
-
You use
obj
as a dictionary. If you want to safeguard against inherited properties, you useobj = Object.create(null)
. Those objects don't even have a.hasOwnProperty
method. And with ES6, aMap
is a better solution anyway. -
You are enumerating array keys. To safeguard against inherited properties from
Array.prototype
, you should not usehasOwnProperty
but rather use the proper loop type that iterates indices instead. -
You are processing JSON.
JSON.parse
'd objects inherit only fromObject.prototype
which does not have enumerable properties anyway. -
You are processing (JSON/dictionary) objects and fear that someone added enumerable properties to
Object.prototype
. Well, that's their fault, not your loop's one. If the environment is broken, your code is as well; there's nothing you can - and need - to do against. -
You are processing arbitrary objects with unknown prototype chains. To skip inherited properties in a
for in
loop, the only safe approach is to useif (Object.prototype.hasOwnProperty.call(obj, key))
or avoid the drama altogether viaObject.keys
/Object.getOwnProperty…
.
The last case is the only one where you'd really need hasOwnProperty
.
The proliferation of if (obj.hasOwnProperty(key))
needs to be stopped,
it's cargo cult programming at best.
, Bergi
Langdon wrote: > Something that simply wraps this all-to-common code: > > var key; > > for (key in obj) { > if (obj.hasOwnProperty(key) === true) { > ... > } > } Imo the problem is not that there is no wrapper method for this pattern, but that this code is all too common. It should rarely be needed, and is mostly wrong anyway. The cases to consider are * You use `obj` as a dictionary. If you want to safeguard against inherited properties, you use `obj = Object.create(null)`. Those objects don't even have a `.hasOwnProperty` method. And with ES6, a `Map` is a better solution anyway. * You are enumerating array keys. To safeguard against inherited properties from `Array.prototype`, you should not use `hasOwnProperty` but rather use the proper loop type that iterates indices instead. * You are processing JSON. `JSON.parse`'d objects inherit only from `Object.prototype` which does not have enumerable properties anyway. * You are processing (JSON/dictionary) objects and fear that someone added enumerable properties to `Object.prototype`. Well, that's their fault, not your loop's one. If the environment is broken, your code is as well; there's nothing you can - and need - to do against. * You are processing arbitrary objects with unknown prototype chains. To skip inherited properties in a `for in` loop, the only safe approach is to use `if (Object.prototype.hasOwnProperty.call(obj, key))` or avoid the drama altogether via `Object.keys`/`Object.getOwnProperty…`. The last case is the only one where you'd really need `hasOwnProperty`. The proliferation of `if (obj.hasOwnProperty(key))` needs to be stopped, it's cargo cult programming at best. Regards, Bergi
I have a proposal I'm finally bringing to es-discuss.
leobalter/object-keysin-valuesin-entries-in (rationale, examples and steps included)
I've already talked to some TC39 members about it and I believe it fits right on this topic.
I have a proposal I'm finally bringing to es-discuss. https://github.com/leobalter/object-keysin-valuesin-entries-in (rationale, examples and steps included) I've already talked to some TC39 members about it and I believe it fits right on this topic. On Mon, Mar 7, 2016 at 5:48 PM, Bergi <a.d.bergi at web.de> wrote: > Langdon wrote: > > Something that simply wraps this all-to-common code: >> >> var key; >> >> for (key in obj) { >> if (obj.hasOwnProperty(key) === true) { >> ... >> } >> } >> > > Imo the problem is not that there is no wrapper method for this pattern, > but that this code is all too common. It should rarely be needed, and is > mostly wrong anyway. > > The cases to consider are > > * You use `obj` as a dictionary. If you want to safeguard against > inherited properties, you use `obj = Object.create(null)`. Those objects > don't even have a `.hasOwnProperty` method. > And with ES6, a `Map` is a better solution anyway. > > * You are enumerating array keys. To safeguard against inherited > properties from `Array.prototype`, you should not use `hasOwnProperty` but > rather use the proper loop type that iterates indices instead. > > * You are processing JSON. `JSON.parse`'d objects inherit only from > `Object.prototype` which does not have enumerable properties anyway. > > * You are processing (JSON/dictionary) objects and fear that someone added > enumerable properties to `Object.prototype`. Well, that's their fault, not > your loop's one. If the environment is broken, your code is as well; > there's nothing you can - and need - to do against. > > * You are processing arbitrary objects with unknown prototype chains. To > skip inherited properties in a `for in` loop, the only safe approach is to > use `if (Object.prototype.hasOwnProperty.call(obj, key))` or avoid the > drama altogether via `Object.keys`/`Object.getOwnProperty…`. > > The last case is the only one where you'd really need `hasOwnProperty`. > The proliferation of `if (obj.hasOwnProperty(key))` needs to be stopped, > it's cargo cult programming at best. > > Regards, > Bergi > > _______________________________________________ > 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/20160308/2c9aea84/attachment.html>
- How is a user supposed to intuit the difference between Object.keys and Object.keysIn? For a non-expert, it sounds like the same thing.
- With "keys", "entries", etc., we lead the user down the "safe" object-as-dict path. Does adding these methods undo some of that leading? What are the compelling use-cases for proto-chain-as-dict?
1. How is a user supposed to intuit the difference between Object.keys and Object.keysIn? For a non-expert, it sounds like the same thing. 2. With "keys", "entries", etc., we lead the user down the "safe" object-as-dict path. Does adding these methods undo some of that leading? What are the compelling use-cases for proto-chain-as-dict? On Tue, Mar 8, 2016 at 3:23 PM Leo Balter <leonardo.balter at gmail.com> wrote: > I have a proposal I'm finally bringing to es-discuss. > > https://github.com/leobalter/object-keysin-valuesin-entries-in > (rationale, examples and steps included) > > I've already talked to some TC39 members about it and I believe it fits > right on this topic. > > On Mon, Mar 7, 2016 at 5:48 PM, Bergi <a.d.bergi at web.de> wrote: > >> Langdon wrote: >> >> Something that simply wraps this all-to-common code: >>> >>> var key; >>> >>> for (key in obj) { >>> if (obj.hasOwnProperty(key) === true) { >>> ... >>> } >>> } >>> >> >> Imo the problem is not that there is no wrapper method for this pattern, >> but that this code is all too common. It should rarely be needed, and is >> mostly wrong anyway. >> >> The cases to consider are >> >> * You use `obj` as a dictionary. If you want to safeguard against >> inherited properties, you use `obj = Object.create(null)`. Those objects >> don't even have a `.hasOwnProperty` method. >> And with ES6, a `Map` is a better solution anyway. >> >> * You are enumerating array keys. To safeguard against inherited >> properties from `Array.prototype`, you should not use `hasOwnProperty` but >> rather use the proper loop type that iterates indices instead. >> >> * You are processing JSON. `JSON.parse`'d objects inherit only from >> `Object.prototype` which does not have enumerable properties anyway. >> >> * You are processing (JSON/dictionary) objects and fear that someone >> added enumerable properties to `Object.prototype`. Well, that's their >> fault, not your loop's one. If the environment is broken, your code is as >> well; there's nothing you can - and need - to do against. >> >> * You are processing arbitrary objects with unknown prototype chains. To >> skip inherited properties in a `for in` loop, the only safe approach is to >> use `if (Object.prototype.hasOwnProperty.call(obj, key))` or avoid the >> drama altogether via `Object.keys`/`Object.getOwnProperty…`. >> >> The last case is the only one where you'd really need `hasOwnProperty`. >> The proliferation of `if (obj.hasOwnProperty(key))` needs to be stopped, >> it's cargo cult programming at best. >> >> Regards, >> Bergi >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > > _______________________________________________ > 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/20160308/2784ce87/attachment.html>
What exactly is the use case for iterating over each key/value pair in the entire prototype chain? Just curious
What exactly is the use case for iterating over each key/value pair in the entire prototype chain? Just curious > On Mar 8, 2016, at 3:35 PM, Kevin Smith <zenparsing at gmail.com> wrote: > > 1. How is a user supposed to intuit the difference between Object.keys and Object.keysIn? For a non-expert, it sounds like the same thing. > 2. With "keys", "entries", etc., we lead the user down the "safe" object-as-dict path. Does adding these methods undo some of that leading? What are the compelling use-cases for proto-chain-as-dict? > > On Tue, Mar 8, 2016 at 3:23 PM Leo Balter <leonardo.balter at gmail.com <mailto:leonardo.balter at gmail.com>> wrote: > I have a proposal I'm finally bringing to es-discuss. > > https://github.com/leobalter/object-keysin-valuesin-entries-in <https://github.com/leobalter/object-keysin-valuesin-entries-in> (rationale, examples and steps included) > > I've already talked to some TC39 members about it and I believe it fits right on this topic. > > On Mon, Mar 7, 2016 at 5:48 PM, Bergi <a.d.bergi at web.de <mailto:a.d.bergi at web.de>> wrote: > Langdon wrote: > > Something that simply wraps this all-to-common code: > > var key; > > for (key in obj) { > if (obj.hasOwnProperty(key) === true) { > ... > } > } > > Imo the problem is not that there is no wrapper method for this pattern, but that this code is all too common. It should rarely be needed, and is mostly wrong anyway. > > The cases to consider are > > * You use `obj` as a dictionary. If you want to safeguard against inherited properties, you use `obj = Object.create(null)`. Those objects don't even have a `.hasOwnProperty` method. > And with ES6, a `Map` is a better solution anyway. > > * You are enumerating array keys. To safeguard against inherited properties from `Array.prototype`, you should not use `hasOwnProperty` but rather use the proper loop type that iterates indices instead. > > * You are processing JSON. `JSON.parse`'d objects inherit only from `Object.prototype` which does not have enumerable properties anyway. > > * You are processing (JSON/dictionary) objects and fear that someone added enumerable properties to `Object.prototype`. Well, that's their fault, not your loop's one. If the environment is broken, your code is as well; there's nothing you can - and need - to do against. > > * You are processing arbitrary objects with unknown prototype chains. To skip inherited properties in a `for in` loop, the only safe approach is to use `if (Object.prototype.hasOwnProperty.call(obj, key))` or avoid the drama altogether via `Object.keys`/`Object.getOwnProperty…`. > > The last case is the only one where you'd really need `hasOwnProperty`. > The proliferation of `if (obj.hasOwnProperty(key))` needs to be stopped, it's cargo cult programming at best. > > Regards, > Bergi > > _______________________________________________ > 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> > > _______________________________________________ > 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> > _______________________________________________ > 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/20160308/c7b99abe/attachment-0001.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/20160308/c7b99abe/attachment-0001.sig>
- How is a user supposed to intuit the difference between Object.keys
and Object.keysIn? For a non-expert, it sounds like the same thing.
I've had funny conversations trying to find the best name for these methods. With _.keysIn, valuesIn and now entriesIn are already present on Lodash, with a similar behavior, I assume it's easier to conciliate them from it.
I tried other names, like enumerateKeys
, allKeys
, etc, I ended up
convinced the current proposal has the better names we can use.
- With "keys", "entries", etc., we lead the user down the "safe"
object-as-dict path. Does adding these methods undo some of that leading? What are the compelling use-cases for proto-chain-as-dict?
Having these methods following the same initial keyword (keys to keysIn, values to valuesIn) makes it a bit easier to tell the extended name method catches the extended properties.
What are the compelling use-cases for proto-chain-as-dict?
As an example, the current Lodash implementation uses _.keysIn
extensively on object cloning and mapping utilities, including variations
of assign functions like _.assign
, _.assignIn
and _.functionsIn
. This
is useful on functional and immutable paradigms. Some of these methods also
use the value pairs. The previous Reflect.enumerate
supported these
methods until it got removed along the [[Enumerate]] internal.
> 1. How is a user supposed to intuit the difference between Object.keys and Object.keysIn? For a non-expert, it sounds like the same thing. I've had funny conversations trying to find the best name for these methods. With _.keysIn, valuesIn and now entriesIn are already present on Lodash, with a similar behavior, I assume it's easier to conciliate them from it. I tried other names, like `enumerateKeys`, `allKeys`, etc, I ended up convinced the current proposal has the better names we can use. > 2. With "keys", "entries", etc., we lead the user down the "safe" object-as-dict path. Does adding these methods undo some of that leading? What are the compelling use-cases for proto-chain-as-dict? Having these methods following the same initial keyword (keys to keysIn, values to valuesIn) makes it a bit easier to tell the extended name method catches the extended properties. > What are the compelling use-cases for proto-chain-as-dict? As an example, the current Lodash implementation uses `_.keysIn` extensively on object cloning and mapping utilities, including variations of assign functions like `_.assign`, `_.assignIn` and `_.functionsIn`. This is useful on functional and immutable paradigms. Some of these methods also use the value pairs. The previous `Reflect.enumerate` supported these methods until it got removed along the [[Enumerate]] internal. On Tue, Mar 8, 2016 at 3:35 PM, Kevin Smith <zenparsing at gmail.com> wrote: > 1. How is a user supposed to intuit the difference between Object.keys and > Object.keysIn? For a non-expert, it sounds like the same thing. > 2. With "keys", "entries", etc., we lead the user down the "safe" > object-as-dict path. Does adding these methods undo some of that leading? > What are the compelling use-cases for proto-chain-as-dict? > > On Tue, Mar 8, 2016 at 3:23 PM Leo Balter <leonardo.balter at gmail.com> > wrote: > >> I have a proposal I'm finally bringing to es-discuss. >> >> https://github.com/leobalter/object-keysin-valuesin-entries-in >> (rationale, examples and steps included) >> >> I've already talked to some TC39 members about it and I believe it fits >> right on this topic. >> >> On Mon, Mar 7, 2016 at 5:48 PM, Bergi <a.d.bergi at web.de> wrote: >> >>> Langdon wrote: >>> >>> Something that simply wraps this all-to-common code: >>>> >>>> var key; >>>> >>>> for (key in obj) { >>>> if (obj.hasOwnProperty(key) === true) { >>>> ... >>>> } >>>> } >>>> >>> >>> Imo the problem is not that there is no wrapper method for this pattern, >>> but that this code is all too common. It should rarely be needed, and is >>> mostly wrong anyway. >>> >>> The cases to consider are >>> >>> * You use `obj` as a dictionary. If you want to safeguard against >>> inherited properties, you use `obj = Object.create(null)`. Those objects >>> don't even have a `.hasOwnProperty` method. >>> And with ES6, a `Map` is a better solution anyway. >>> >>> * You are enumerating array keys. To safeguard against inherited >>> properties from `Array.prototype`, you should not use `hasOwnProperty` but >>> rather use the proper loop type that iterates indices instead. >>> >>> * You are processing JSON. `JSON.parse`'d objects inherit only from >>> `Object.prototype` which does not have enumerable properties anyway. >>> >>> * You are processing (JSON/dictionary) objects and fear that someone >>> added enumerable properties to `Object.prototype`. Well, that's their >>> fault, not your loop's one. If the environment is broken, your code is as >>> well; there's nothing you can - and need - to do against. >>> >>> * You are processing arbitrary objects with unknown prototype chains. To >>> skip inherited properties in a `for in` loop, the only safe approach is to >>> use `if (Object.prototype.hasOwnProperty.call(obj, key))` or avoid the >>> drama altogether via `Object.keys`/`Object.getOwnProperty…`. >>> >>> The last case is the only one where you'd really need `hasOwnProperty`. >>> The proliferation of `if (obj.hasOwnProperty(key))` needs to be stopped, >>> it's cargo cult programming at best. >>> >>> Regards, >>> Bergi >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> >> _______________________________________________ >> 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/20160308/45e14d8e/attachment.html>
My apologies if this has been discussed before (I have to imagine it has, but couldn't find anything).
Why isn't there a
forIn
method on Object natively?Something that simply wraps this all-to-common code:
var key;
for (key in obj) { if (obj.hasOwnProperty(key) === true) { ... } }
Example: jsfiddle.net/langdonx/d4Lph13u
TIA, Langdon