> Le 13 oct. 2016 à 19:20, Bob Myers <r...@gol.com> a écrit :
> Why is this needed? Why are people trying to get the property of an object 
> which is null? Why is the object null in the first place?

This is not about trying to get something from null, but about taking different 
paths according to when a reference is null or not without needing to assign to 
temporary variables, writing complete `if` structures, and/or repeating oneself.

> (...)
> Just as an example, consider the following idiom for null propagation:
> ```
> a ? a.b ? a.b.c : undefined : undefined
> ```
> We can leverage this pattern by allowing the `:` in the ternary operator to 
> be omitted (defaulting to undefined), allowing us to write:
> ```
> a ? a.b ? a.b.c
> ```
> Whether you love it or hate it, at least this solves more problems that just 
> null propagation. I'm not seriously suggesting this. I'm just saying we need 
> to be more creative in brainstorming possible solutions to the problem.

You can already write`a && a.b && a.b.c` in JS... but you still have to repeat 
`a` thrice and`b` twice, which is an issue if `a` and `b` are complex or 
lengthy expressions. Sometimes I am tempted to write something in the lines of 
`(_ = a.b) && _.c` in order to avoid the issue, but it is less readable.

Here is a more complex although somewhat contrived example: If an <input> 
element named "foo" is inside a <details> section, open the latter in order to 
reveal the former:

document.querySelector("input[name=foo]")?.closest(".details")?.open = true

(The short-circuiting mechanism, which is an important part of the semantics in 
my proposal, ensures that the assignment is performed only when the expressions 
just before the `?`s are not null/undefined.)

es-discuss mailing list

Reply via email to