Created
May 2, 2011 13:13
-
-
Save bodbdigr/951593 to your computer and use it in GitHub Desktop.
defaults for javascript
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
(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