Short Comparing proposal
Felipe Nascimento de Moura
felipenmoura at gmail.com
Wed Feb 1 17:03:56 UTC 2017
I see...
we could perhaps think on some other approach.
What about:
if (obj.prop.prop ?== 'a' : 'b')
This way, we have "?==", which is not too chocking once we already got
"!==", and this kinda prepares the engine to deal with the next values as
part of the comparison.
Also, feels familiar due to " something ? 'a' : 'b' "
Personally, I don't feel that comfortable with this syntax.
We could think on different a token as well
if (obj.prop.prop === (? 'a' : 'b')) // feels like a ternary, but without
the first statement
if (obj.prop.prop === ('a' | 'b'))) // could break the web once it is
already valid syntax (although not very used)
if (obj.prop.prop === ('a' :: 'b')) // perhaps a double colon
Anyways, open for suggestions :)
On Wed, Feb 1, 2017 at 2:48 PM Ryan Birmingham <rainventions at gmail.com>
wrote:
> 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/12978e0f/attachment.html>
More information about the es-discuss
mailing list