Skip to content

Instantly share code, notes, and snippets.

@turbobabr
Last active November 22, 2016 03:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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