Click to See Complete Forum and Search --> : Accessing Unknown Global Variables?


hnorris
02-12-2003, 01:33 PM
Hello -

Is there a way to access global variables, whose names are not already known, including globally defined function names?

Using the for/in technique on the global window object doesn't seem to work as expected as the global x variable is never shown in this loop:

var x = "xVar";
function showGlobalProperties() {
for(var prop in window) alert("Property " +prop);
}

HOWEVER -

alert(window.x);
OR
alert(window["x"] );

works fine, as expected.

For/in seems to work fine on objects that I've created.
- unclear though why it doesn't seem to work with predefined objects. BTW, my test platform is IE6 on Win2k. Thanks for your thoughts!

- Hnorris

hnorris
02-12-2003, 06:24 PM
Hi Dave -

Thanks for your thoughts - I also attempted to print out local function variables, also without success - see code below and local var y. - Hnorris

/************************************************/
function ootest() {
var y = "yVar"; // this is not shown in the prop loop
this.x = "xVar";

this.props = function() {
alert("GOT2 props()");
for(var prop in this)
alert("Prop " +prop +", val " +this[prop]);

// Can we find a loop to show local function variables, like y??
var o = ootest.prototype;
for(var prop in o)
alert("Prop " +prop +", val " +o[prop]);
};
}

/************************************************/
function test() {
var o = new ootest();
o.props();
}