Skip to content

Instantly share code, notes, and snippets.

@bodbdigr
Created May 2, 2011 13:13
Show Gist options
  • Save bodbdigr/951593 to your computer and use it in GitHub Desktop.
Save bodbdigr/951593 to your computer and use it in GitHub Desktop.
defaults for javascript
(function(w) {
w._ = w._ || {};
// Wraps given function with default arguments
//
// example:
//
// var someFunc = function() { return arguments };
//
// someFunc = _.defauls(someFunc, ['1st', 'second', 'some other', 1, 'last one'])
//
// someFunc(7,8) // => returns [7, 8, 'some other', 1, 'last one']
// someFunc() // => returns ['1st', 'second', 'some other', 1, 'last one']
_.defaults = function(fn, defaults) {
if (!_.isFunction(fn)) throw new TypeError("first param should be a function");
defaults = defaults || [];
return function() {
var args = Array.prototype.call(arguments, 0);
Array.prototype.push.apply(args, defaults.slice(args.length, defaults.length));
return fn(args);
};
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment