Couldn’t you just do cmd.run || cmd.fire || cmd.execute || cmd.callback ? I’m not super familiar with js. Worked with it a bit, but still not great, so please correct me if I’m wrong.
Nowadays you could also use the null coalesce operator ?? which would be preferable to || since logical or can be confused by false. For example, if cmd.fire is false, the result of || would be cmd.callback, the result of ?? would be cmd.fire.
That’s true, but in this case where you are seemingly wanting to save a function reference to call later, || would probably be the preferred method, seeing as you would be more likely to find an actual function, rather than a Boolean value. Otherwise later on it might equate to calling false(). Just my thoughts. Again, I’m not super familiar with js. My day job is mobile apps in Flutter/Dart.
?? would be better in this case if its expected to be a funftion or undefined/null, a function or false is a weird type to use, function or undefined / null makes more sense
The typing might make more sense, but if there happens to be a point where it isn’t the typing you expect, wouldn’t || help correct for that and make sure it comes back to the correct type?
If you've got false where you expect a function reference, you'd want to find out about it sooner rather than later. When you try execute run() and the browser complains that false isn't a function, that may be easier to debug than simply, "why is the wrong function running?"
That’s fair. I suppose I’m looking at it like “all these functions are equivalent, but might be given different names in different areas of the code because we’ve had 5 developers in the past 2 years”
166
u/Koala_King_ Apr 22 '21
Couldn’t you just do
cmd.run || cmd.fire || cmd.execute || cmd.callback? I’m not super familiar with js. Worked with it a bit, but still not great, so please correct me if I’m wrong.