Skip to content

Instantly share code, notes, and snippets.

@tswistak
Created March 15, 2018 08:12
Show Gist options
  • Save tswistak/38ff72de01aafd327a28317eddec2632 to your computer and use it in GitHub Desktop.
Save tswistak/38ff72de01aafd327a28317eddec2632 to your computer and use it in GitHub Desktop.
Execute function only if defined
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