For example, if we assume the following code:
var f = function() { return 'hello world' };
var x = 10;
var y = 314;
var g = function() {
var buf = [], xx = x;
while (xx--)
buf.append(f() + ' ');
return buf.join('');
}
I can get the actual "code" as a string of g
with g.toString()
. However, this does not (obviously) get f
and x
—members of the closure of g
(sorry if I'm not quite using these terms correctly.)
Is there some way to query a function for what its closure contains? Ideally I could get an object like:
{ 'f' : f, 'x': x } // note that `y` is not here
If I have to drop into C++ for special interactions with V8, that's okay—although somehow doing this in pure JavaScript would be best.
I know that this is a bit of an odd question—but I do have a legitimate reason for wanting this!
x
thing was my fault. Wasn't thinking correctly. Andf
is a member because it is required forg
to run (buf.append(f()...)
) just likex
is a member for the same reason.y
is not there.