Short Comparing proposal

Ryan Birmingham rainventions at gmail.com
Wed Feb 1 16:47:59 UTC 2017


I like the idea, and I generally agree that your proposal would increase
readability, but I'm not a fan of the colon in parenthesis.
In this context, the colon looks like it should represent a member
assignment of 'b' to 'a'.


-Ryan Birmingham

On 1 February 2017 at 11:27, Felipe Nascimento de Moura <
felipenmoura at gmail.com> wrote:

> Hi.
>
> I wonder if there is already a proposal for such thing or if it would be
> something interesting to be proposal:
>
> *The motivation:*
> Let's say you have an object whose property you want to compare to 'a' or
> 'b':
>
> ```
> if (object.property.secondProp === 'a' || object.property.secondProp ===
> 'b') {
>   // do something
> }
> ```
>
> *The proposal:*
> Now, with this syntax you would be able to perform such comparisons in a
> much simpler, better to read approach:
>
> ```
> if (object.property.secondProp === ('a' : 'b')) {
>     // do Something
> }
> ```
>
> And with the advance of the Optional Chaining Operator
> <https://github.com/claudepache/es-optional-chaining> proposal, it would
> get even better, like so:
>
> ```
> // this:
> if (object &&
>     object.property &&
>     (object.property.secondProp === 'a' ||
>     object.property.secondProp === 'b')) {
>   // ...
> }
>
> // becomes this:
> if (object?.property?.secondProp === ('a' : 'b')) { ... }
> ```
>
> *Alternatives:*
> I do know that we could accomplish that with other techniques, like:
>
> ```
> ['a', 'b'].includes(object.property.secondProp)
> ```
>
> I just think it might be counterintuitive and having a syntax for a "short
> comparing" would be better for reading and understanding.
>
> Would make ternary comparison even shorter, too:
>
> ```
> let currentState = object.property.status === ('fail' : 'ok') ? 'done' :
> 'doing'
> ```
>
> Please let me hear your thoughts on that :)
>
> Regards.
>
>
>
>
>
>
>
> _______________________________________________
> 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/20170201/51735f38/attachment-0001.html>


More information about the es-discuss mailing list