Last active
November 22, 2016 03:23
-
-
Save turbobabr/11106163 to your computer and use it in GitHub Desktop.
Underscore.js isDefined mixin
This file contains 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
// 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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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