how to get the list of local variables

S

schettino72

hi,

how can i get a list of all local variables defined in a function?

cheers,
Eduardo
 
T

Thomas 'PointedEars' Lahn

schettino72 said:
how can i get a list of all local variables defined in a function?

There is no access to the local Variable Object. So you would have to rely
on parsing the string representation of the Function object, which is left
as an exercise to the reader.


PointedEars
 
E

Evertjan.

schettino72 wrote on 23 aug 2007 in comp.lang.javascript:
how can i get a list of all local variables defined in a function?

You cannot, I think, not with js that is.

Why would you need that for?
 
S

schettino72

Thanks for the replies guys.
Why would you need that for?

I writing a unit test framework for javascript. Test cases are
defined by extending my TestCase base class. I want to automatically
find all defined test cases, so I need to introspect the code
somehow.
I solved my problem putting the test cases out of my "function" and
doing

for (var o in top){
....
 
T

Thomas 'PointedEars' Lahn

schettino72 said:
I writing a unit test framework for javascript. Test cases are
defined by extending my TestCase base class.

Chance is that there is no class.
I want to automatically find all defined test cases, so I need
to introspect the code somehow.
I solved my problem putting the test cases out of my "function" and
doing

for (var o in top){
....

Given that the `in' operator is not supported before JavaScript 1.4,
JScript 5.0, ECMAScript 3, and that `top' is a host-defined property of
the Global Object, are you sure the results your test framework yields
are always correct?


PointedEars
 
S

schettino72

Given that the `in' operator is not supported before JavaScript 1.4,
JScript 5.0, ECMAScript 3,
I am not worried about this...
and that `top' is a host-defined property of
the Global Object,
this was quick and dirty. i will probably load them in my own object/
namespace
are you sure the results your test framework yields
are always correct?
it worked fine with what i have in hand. but "always" is a too strong
word ;)

thanks for your comments.
eduardo
 
T

Thomas 'PointedEars' Lahn

schettino72 said:
this was quick and dirty. i will probably load them in my own object/
namespace

You can use `this' for referring to the global execution context in the
local execution context. You can then assign `this' to a global variable
and then use that variable locally.

var _global = this;

function ...()
{
... _global ...
}

However, using a user-defined object instead is a Good Idea[tm].

Please always provide attribution of quoted material.


Regards,

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top