For-has-in loop

Mike Samuel mikesamuel at gmail.com
Mon Feb 19 20:46:45 UTC 2018


On Mon, Feb 19, 2018 at 1:50 PM, Sebastian Malton <sebastian at malton.name>
wrote:

> I would like to propose that the following be valid since it is a very
> common use case.
>
> ```
> for (const field has in object) {
>     ....
> }
> ```
>
> Would be equivalent to:
>
> ```
> for (const field in object) {
>     if (!object.hasOwnPropety(field)) {
>         continue;
>     }
>     ....
> }
> ```
>

I'm generally supportive of getting rid of error-prone calls to
hasOwnProperty methods
since they fail when there is a "hasOwnProperty" field anywhere on the
prototype chain
between object and Object.prototype.

This idiom looks like something that usually almost means what its author
intends but rarely
exactly.

Are there any proposals for an `in` operator variant that only checks for
own properties?
If so, the syntax could be synchronized with that.
(I thought there was one but couldn't find it on
https://github.com/tc39/proposals )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180219/36f3e2cf/attachment.html>


More information about the es-discuss mailing list