I think one could make the case it adds value by reducing the need to repeat the same name twice, thus reducing the potential for errors.
I'm not sure it's worth it given how nontraditional it is, and I think JS devs typically do not think of functions as objects as it is being used here, but still it's an interesting approach
This specific scenario isn't all that convincing, but IMO in general it's safer to be more expressive with less repetition than not, because it reduces the opportunity for subtle errors in the future.
The Elvis operator on a function says "if this exists, call it", just like when you use it to get a property of an object that might be null or undefined.
Just because the operator is unfamiliar to you, doesn't mean it's bad. It just means that it's unfamiliar.
if(typeof myFunction === âfunctionâ)
Is significantly longer and says exactly the same thing.
Why fight language improvements? Do you disallow spread because youâre used to Object.assign? Or mutate vars inside for loops instead of Array.map? I assure you if you use optional chaining youâll find it reads far easier especially when accessing deeply nested properties on objects. 4 props or more deep is pretty hideous with &&
Its not as nontraditional when you consider its literally just the elvis operator but applied to function calls. Like it doesnt really so anything surprising
58
u/dudeitsmason Nov 10 '20
Just because you can, doesn't mean you should