If the variables are not globally, it is still possible, but not practical.
function somefunc() { var a = 'aaa', b = 'bbb'; var compact = function() { var obj = {}; for (var i = 0; i < arguments.length; i++) { var key = arguments[i]; var value = eval(key); obj[key] = value; } return obj; } console.log(compact('a', 'b'))
The good news is ES6 has a new feature that will do just that.
var a=1,b=2; console.log({a,b})
source share