Last active
December 21, 2015 13:08
-
-
Save lcaballero/6310213 to your computer and use it in GitHub Desktop.
Example Mapping Parameter Names to Call Values
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() { | |
var slice = Array.prototype.slice | |
var console = window["console"] || {console:{}}; | |
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | |
function getParamNames(func) { | |
var fnStr = func.toString().replace(STRIP_COMMENTS, '') | |
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(/([^\s,]+)/g) | |
if(result === null) | |
result = [] | |
return result; | |
}; | |
function mapParameters(func, args) { | |
var a = getParamNames(func); | |
var mapping = {}; | |
for (var i = 0; i < a.length; i++) { | |
var g = args[i]; | |
mapping[a[i]] = g; | |
} | |
mapping["..."] = slice.call(args, a.length); | |
return mapping; | |
}; | |
console.params = function() { | |
var args = arguments.callee.caller.arguments | |
var mapping = mapParameters(arguments.callee.caller, slice.call(args)); | |
console.log(mapping); | |
} | |
})(); | |
(function(a, b, c) { | |
console.params(); | |
})(1, 2, 3, 4, 5, 6, false, {}); | |
// Object {a: 1, b: 2, c: 3, ...: Array[5]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment