Skip to content

Instantly share code, notes, and snippets.

@leoneed
Created May 15, 2015 09:58
Show Gist options
  • Save leoneed/0e1d77d4bd8d9ede76e3 to your computer and use it in GitHub Desktop.
Save leoneed/0e1d77d4bd8d9ede76e3 to your computer and use it in GitHub Desktop.
Function.prototype.default = function() {
var that = this,
defArgs = arguments,
count = arguments.length;
return function() {
var args = Array.prototype.slice.call(arguments);
if (count) {
for (var i=0; i<count; ++i) {
if(typeof args[i] === 'undefined') {
args[i] = defArgs[i];
}
}
}
that.apply(this, args);
}
}
var test = function(a, b, c) {
console.log(a, b, c);
}.default(1, 'huy', [2,3,4]);
test();
test('xcv', 11);
test({},[],1234567,'gello');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment