Skip to content

Instantly share code, notes, and snippets.

@subzey
Created December 3, 2012 16:42
Show Gist options
  • Save subzey/4196191 to your computer and use it in GitHub Desktop.
Save subzey/4196191 to your computer and use it in GitHub Desktop.
Function.prototype.kwargs
Function.prototype.kwargs = function()
{
var argNames, kwargs, args = [], thisObject;
for (var i=0; i<arguments.length; i++){
var obj = arguments[i];
if (Object(obj) !== obj){
continue;
}
for (var key in obj){
var index;
if (key === "this"){
thisObject = obj[key];
continue;
}
if (isFinite(key)){
index = key;
} else {
argNames = argNames || Function.prototype.toString.call(this).match(/\((\s*.*?\s*)\)/)[1].split(/,\s*/);
index = argNames.indexOf(key);
if (index === -1){
kwargs = kwargs || {};
kwargs[key] = obj[key];
continue;
}
}
args[index] = obj[key];
}
}
if (kwargs){
args.length = Math.max(args.length, argNames.length, this.length);
args.push(kwargs);
}
this.apply(thisObject, args);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment