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!
I found this discussion of V8 closure implementations enlightening. It sounds like the C++ object you're looking for is a Context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With