3 ms·
> let state = typeof behavior.init === "function" ? behavior.init() : {}; Javascript is such a mess itself, i don't know how one can explain something in it
by Dolores12 9y ago
> let state = typeof behavior.init === "function" ? behavior.init() : {};
Javascript is such a mess itself, i don't know how one can explain something in it. Choose python, reads like English.
- qaq 9y agoYou can write ugly code in any lang.
- faitswulff 9y agoIt's not unique to javascript. The equivalent in ruby would be `state = behavior.init.class = Method ? behavior.init : {}` After looking it up, I don't disagree that Python looks a lot prettier, though: `state = behavior.init() if callable(behavior.init) else {}` That made me curious if Ruby had anything like Python's `callable`, but I couldn't find anything. This is my best effort in Ruby for aesthetics: `state = if behavior.init.respond_to? :call then behavior.init() else {} end` In the end, I agree, Javascript looks much worse than the equivalent Python in this case. Unfortunately I don't get a choice of python on the frontend :p
- jack_jennings 9y agoCloser to idomatic Ruby: state = behavior.respond_to? :init ? behavior.init : {} Asking an object if something is "callable" in Ruby is probably unnecesarily verbose, as you are most likely dealing with a method for any attribute access.
- Can_Not 9y agoSlower, poor portability to android/iOS/browser, no typescript/flow (no, those things aren't like typescript or flow), async/await is an afterthought instead of a cornerstone, just because you didn't like an obscure one liner out of context, no thanks.
- seangrogg 9y agoRealistically, if he weren't opting to be as terse as possible, this could be rewritten as: var state; if (typeof behavior.init === "function") { state = behavior.init(); } else { state = {}; } I do enjoy terse ternary operations but they can get very ugly very quickly. I often stick if/else if I'm writing code for others. As far as choosing another language, write Python that gets executed on Chrome without further trans/compilation and I'll happily consider it.