Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple method of creating functions with default arguments.
Function.prototype.withDefaults = function () {
var defaults = [].slice.call(arguments);
var f = this;
return function () {
arguments.__proto__ = defaults;
arguments.length = f.length;
return f.apply(this, arguments);
};
};
// Example
var g = function (a, b, c) {
return [a, b, c].join();
}.withDefaults(1, 2, 3);
g(); // "1,2,3"
g(4); // "4,2,3"
g(4,5); // "4,5,3"
g(4,5,6); // "4,5,6"
@vinilaraa

This comment has been minimized.

Copy link

vinilaraa commented Apr 2, 2015

greaat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.