Skip to content

Instantly share code, notes, and snippets.

@turbobabr
Last active November 22, 2016 03:23
Show Gist options
  • Save turbobabr/11106163 to your computer and use it in GitHub Desktop.
Save turbobabr/11106163 to your computer and use it in GitHub Desktop.
Underscore.js isDefined mixin
// Underscore.js isDefined mixin
// Returns true if value is defined.
_.mixin({
isDefined: function(reference) {
return !_.isUndefined(reference);
}
});
/* Example:
var obj = {
definedProperty: 10
};
_.isDefined(obj.definedProperty);
=> true
_.isDefined(obj.aBunchOfCrazyMonkeysProperty);
=> false
*/
@turbobabr
Copy link
Author

I wrote this function because I personally hate the if(!_.isUndefined(....)) {} expression. I believe that isDefined() is more clear for a reader. Have no idea why it's missing in Underscore framework itself. Inspired by angular.isDefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment