Created
May 21, 2015 22:06
-
-
Save Error601/11b0554a6ba7062c32e1 to your computer and use it in GitHub Desktop.
Return first defined value from argument list.
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
/*! | |
* Return the first defined value from argument | |
* list. Vars must be declared before passing | |
* to the function - set equal to self to catch | |
* any existing value for that var name. Useful | |
* for fetching values from an outer scope that | |
* may or may not be defined yet, while providing | |
* a fallback of a known (explicitly) defined value. | |
* | |
* // vars declared and assigned to self | |
* var foo=foo, bar=bar; | |
* var name = firstDefined(foo, bar, 'Bob'); | |
* // returns 'Bob' since foo and bar are undefined | |
*/ | |
function firstDefined() { | |
var undefined, i = -1; | |
while (++i < arguments.length) { | |
if (arguments[i] !== undefined) { | |
return arguments[i]; | |
} | |
} | |
return undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment