// Combiner of passed arguments and defaults (usable with any function)
Object.prototype.combine = function(_args){
for(var i in this) {
if(typeof _args[i] == "undefined") {
_args[i] = this[i];
}
}
};
// Specific example function with defaults
function feedTheCat(args){
var defaults = {
'morning' : "nothing",
'noon' : "pork",
'nite' : "mouse"
};
defaults.combine(args);
}
// USAGE: only passing 2 of 3 arguments
feedTheCat({
'morning': "milk",
'nite': "kitekat"
});
// Values would be: milk | pork | kitekat
Default Arguments for Functions
Chris Coyier
on
Thanks, perfect solution.
or $.extend in jQuery
FIX BUG!
Is preferably to not use an prototype for Object, you will have issues when will try to foreach it, this prototype will appear in the list of all objects and you will have to use hasOwnProperty to omit it, so better use simple functions.