Optional Chaining (aka Existential Operator, Null Propagation)

Claude Pache claude.pache at gmail.com
Thu Feb 4 21:10:34 UTC 2016


> Le 4 févr. 2016 à 21:03, Kevin Smith <zenparsing at gmail.com> a écrit :
> 
> 
> That aside, I have a question about the semantics.  What does this do:
> 
> ({ x: 1 }).x?.y.z;
> 
> Does it throw a ReferenceError?
> 

Yes: the `?.` operator does not change the meaning of the subsequent `.` operator. I like to think of it as: the effect is local (short-circuiting aside), you are not switching between "modes". It’s a feature.

Maybe I should write a desugaring in my proposal. The expression `({ x: 1 }).x?.y.z` is equivalent to:

```
(function () {
    var $1 = { x: 1 }).x;
    if ($1 == null)
        return undefined;
    return $1.y.z;
})()
```

—Claude


More information about the es-discuss mailing list