The dot notation is great (object?.property). However, it seems a little strange for bracket notation (object?.['property']) and function invocation (exampleFunction?.(argument)). Indeed, if you were to drop the ? in either, and run it in JavaScript today, an exception would be thrown.
I'd have hoped that JavaScript engines could handle brackets and functions with only the ?, allowing these three forms:
The proposal authors have briefly justified this in their FAQs:
We don’t use the obj?[expr] and func?(arg) syntax, because of the difficulty for the parser to distinguish efficiently those forms from the conditional operator, e.g. obj?[expr].filter(fun):0 and func?(x - 2) + 3 :1.
Unfortunately, I'm not familiar with those other languages. I'd be curious to see their syntax in action if you could point me in their direction.
Just to be clear though, the dot notation in itself is perfectly fine where it makes sense. In fact, it would be quite odd if it were missing in those contexts. However, the proposal is to use the dot notation in places where the . would throw an exception if the ? were missing: for bracket notation and function invocations. For instance, the following code will throw an exception in your browser:
It's in contexts like these that it would be nicer to omit the .. So:
object?.property; // This is perfectly fine.
object?.['property']; // This is a little strange and inconsistent.
object?['property']; // This would make more sense and be consistent with the rest of the language.
In the languages that you hinted at, is the second or third syntax used for bracket notation?
That's possible. I assumed it was for performance reasons because they used the term 'efficiently'. Perhaps, as you suggest, they mean 'efficient to implement', meaning it's simple to implement.
Hmm, I'm not sure. The purpose of optional chaining is not to check whether something exists as such, but rather to access it if it does exist, and return undefined if it does not. Thus, you might want to invoke a function if it exists, and optional chaining would allow you to do so safely without additional code. Plus, in most other parts of the language, () invokes a function or instantiates an instance of a class. Because of this, it would be inconsistent and confusing if, in optional chaining, the same syntax didn't invoke the function, but simply checked for the existence of it. You might also want to retrieve the function if it exists instead of invoking it, in which case you would not include the ().
1
u/Bluecewe Feb 20 '18 edited Feb 20 '18
The dot notation is great (
object?.property). However, it seems a little strange for bracket notation (object?.['property']) and function invocation (exampleFunction?.(argument)). Indeed, if you were to drop the?in either, and run it in JavaScript today, an exception would be thrown.I'd have hoped that JavaScript engines could handle brackets and functions with only the
?, allowing these three forms:The proposal authors have briefly justified this in their FAQs:
Hopefully this difficulty can be overcome for the final standard. There's a lot of discussion about this in the proposal repository. It would be nice to avoid awkward syntax like the current proposal.