Created
January 14, 2014 04:27
-
-
Save poying/8413028 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var utils = module.exports = {}; | |
utils.args = function (defval, names) { | |
defval || (defval = {}); | |
names || (names = []); | |
var map = {}; | |
names.forEach(function (val) { | |
map[val.length] = val; | |
}); | |
return function (args) { | |
var newargs = Object.create(defval); | |
var names = map[args.length]; | |
if (!names) { | |
throw new Error('Wrong arguments'); | |
} | |
names.forEach(function (name, index) { | |
newargs[name] = args[index]; | |
}); | |
return newargs; | |
}; | |
}; | |
utils.function = function (defval, names, fn) { | |
var getArgs = utils.args(defval, names); | |
return function () { | |
return fn.call(this, getArgs(arguments)); | |
}; | |
}; |
Author
poying
commented
Jan 14, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment