Created
March 15, 2018 08:12
-
-
Save tswistak/38ff72de01aafd327a28317eddec2632 to your computer and use it in GitHub Desktop.
Execute function only if defined
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const a = undefined; | |
| // 1 | |
| if (typeof a === 'Function') a(); | |
| // 1 with jQuery | |
| if ($.isFunction(a)) a(); | |
| // same in Lodash -> _.isFunction | |
| // 2 | |
| (a || Function)(); | |
| // 3 | |
| a && a(); | |
| // 3 with jQuery/Lodash | |
| $.isFunction(a) && a() | |
| // 4 with Lodash | |
| _.invoke(a, 'call'); // or _.result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment