Skip to content

Instantly share code, notes, and snippets.

@danrichards
Last active February 21, 2016 04:46
Show Gist options
  • Save danrichards/8df22221d67ffb64954f to your computer and use it in GitHub Desktop.
Save danrichards/8df22221d67ffb64954f to your computer and use it in GitHub Desktop.
_.default(value, default_val) Provide a default value if param is undefined or null.
/**
* Use default vlaue
*/
_.mixin({
default: function(value, default_val) {
if (value === null && typeof value === "object") {
return default_val;
}
return _.isUndefined(value) || _.isNull(value) ? default_val : value;
}
});

Using for default arguments.

var myFunc = function(msg, opt1, opt2) {
    opt1 = _.default(opt1, ' more');
    opt2 = _.default(opt2, ' and even more');
    alert(msg+opt1+opt2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment