Last active
August 29, 2015 14:04
-
-
Save IrakliJani/1fb6c28a768d76017c66 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 defaultArguments(func, defaults) { | |
return function () { | |
var params = Array.prototype.slice.call(arguments); | |
var args = func | |
.toString() | |
.match(/\((.*)\)/)[1] | |
.split(/\s*,\s*/) | |
.filter(Boolean) | |
.map(function (e, i) { return params[i] || defaults[e]; }); | |
return func.apply(null, args); | |
}; | |
} | |
function sum (a, b) { return a + b; } | |
sum = defaultArguments(sum, { a: 10, b: 20 }); | |
console.log(sum(1, 2)); | |
sum = defaultArguments(sum, { a: 10, b: 20 }); | |
console.log(sum(1, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment