3 ms·
Functions are first-class values, so what's wrong with: (someCondition? showFoo : hideFoo)(); You can also pass in arguments if they both accept the same
by chriswarbo 12y ago
Functions are first-class values, so what's wrong with:
(someCondition? showFoo : hideFoo)();
You can also pass in arguments if they both accept the same parameters, eg.
(someCondition? show : hide)(foo, duration);
- masklinn 12y agoExcept they're methods so you have to write (someCondition ? foo.show : foo.hide)(); except the language does not auto-bind so you have to write (someCondition ? foo.show : foo.hide).call(foo); or maybe (someCondition ? foo.show.bind(foo) : foo.hide.bind(foo))();
- chriswarbo 12y agoOr: foo[someCondition? 'show' : 'hide'](); Although looking up functions using raw strings is less than elegant. I'd be tempted to abstract the pattern using a function, but at that point I'd be doing functional programming anyway and wouldn't be using methods ;)