Ternary operator enhancement proposal

Isiah Meadows contact at isiahmeadows.com
Fri Nov 15 17:27:56 UTC 2019


Also, this particular use case could be addressed by adding an
equivalent of the stage 3 optional chaining operators to the proposed
pipeline operator: https://github.com/tc39/proposal-pipeline-operator.

In this light, I've also filed an issue there:
https://github.com/tc39/proposal-pipeline-operator/issues/159

-----

Isiah Meadows
contact at isiahmeadows.com
www.isiahmeadows.com

On Thu, Nov 14, 2019 at 4:21 AM Cyril Auburtin <cyril.auburtin at gmail.com> wrote:
>
> the `do` operator might be good enough
>
> ```js
> const z = do { const z = x?.y?.z; z ? doSomethingWithValue(z) : null; };
> ```
>
> On Wed, Nov 13, 2019 at 3:48 PM Claude Pache <claude.pache at gmail.com> wrote:
>>
>>
>>
>> Le 13 nov. 2019 à 03:43, devlato <ecma6 at devlato.com> a écrit :
>>
>> Hey folks,
>>
>> not sure if you haven't discussed something similar, but what do you think about making an enhancement for the ternary operator, to make it more powerful?
>> I've created a small explanatory doc on GitHub: https://github.com/devlato/proposal-ternary-placeholder
>>
>> Warmest regards,
>> Denis
>>
>>
>> The generic problem is: you do some computation, and you want to reuse the result of that computation later.
>>
>> The generic solution is to use a temporary variable:
>>
>> ```js
>> // declare a temporary variable at the top of some scope
>> var _;
>>
>> // later, use it:
>> const X = () => (_ = x?.y?.z) ? doSomethingWithValue(_) : null;
>> ```
>>
>> Now, you are not satisfied with the existing solution, and you want to avoid declaration of temporary variables in some outer scope...? Maybe:
>>
>> ```js
>> const X = () => (#1 = x?.y?.z) ? doSomethingWithValue(#1) : null;
>> ```
>>
>> (Any similarity with https://developer.mozilla.org/en-US/docs/Archive/Web/Sharp_variables_in_JavaScript is not at all fortuitous.) However, I’m not convinced that the need to declare your temporary variables is such a problem that it is worth the introduction of new syntax.
>>
>>
>> —Claude
>> _______________________________________________
>> 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


More information about the es-discuss mailing list