A very quick stab at an AST solution.
(This is my first play with Esprima, so be gentle! I'm absolutely sure this can be improved)
<style>
pre {
font-size: 8pt;
font-family: Lucida Console, monospace;
}
</style>
<pre id="ast"></pre>
<script src="esprima.js"></script>
<script>
function getvars(fnStr) {
function _getvars(body) {
if (!body) {
return;
}
if (body.length) {
for (var i = 0; i < body.length; i++) {
_getvars(body[i]);
}
} else if ("VariableDeclaration" === body.type) {
for (var i = 0; i < body.declarations.length; i++) {
vars.push(body.declarations[i].id.name);
}
} else if (body.body) {
_getvars(body.body);
}
}
var vars = [];
var syntax = esprima.parse(fnStr);
_getvars(syntax.body);
document.getElementById("ast").innerHTML = JSON.stringify(syntax, null, 4);
return vars;
}
function myfn() {
var a = 1, b = 2, ob = { name: "ob" };
for (var i = 0; i < 10; i++) {
var s = "" + i;
}
getvars(myfn.toString()).forEach(function(___var) {
var ___ob = eval(___var);
console.log(___var, (typeof ___ob), eval(___ob));
});
}
myfn();
</script>
Will print to the console:
a number 1 local-vars-in-function.html:44
b number 2 local-vars-in-function.html:44
ob object Object {name: "ob"} local-vars-in-function.html:44
s string 9 local-vars-in-function.html:44
If you only want the var names, and not the values, then you wouldn't need the inline reporting.