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 <[email protected]> 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 <[email protected] > <mailto:[email protected]>> 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 <[email protected] > <mailto:[email protected]>> 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 > [email protected] <mailto:[email protected]> > https://mail.mozilla.org/listinfo/es-discuss > <https://mail.mozilla.org/listinfo/es-discuss> > > _______________________________________________ > es-discuss mailing list > [email protected] <mailto:[email protected]> > https://mail.mozilla.org/listinfo/es-discuss > <https://mail.mozilla.org/listinfo/es-discuss> > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss
signature.asc
Description: Message signed with OpenPGP using GPGMail
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

