View Full Version : is there trick to access function local var ?
Hello
just wandering ..
is there any way to access function local var without passing it as arg ? some wird trick?
like if i have :
-------------------------------------
function foo1()
{
alert(....here i want to alert the foo2 z var ....);
}
function foo2(x)
{
var z=x;
}
foo2(5) ;
foo1();
-----------------------------------------------
thanks
joh6nn
07-13-2002, 09:10 PM
this might do what you want, but i can't imagine a use for it:
foo.bar = "some value";
function foo(x) {
this.bar = x;
}
function bar() {
alert(foo.bar);
}
i didn't test that at all, and the logic i'm basing it on is a bit sketchy, so you may have to play with it a bit, and it may just plain not work.
adios
07-14-2002, 12:20 AM
You can't access a local variable in a function while that function isn't running - for the simple reason that it doesn't exist. Local (function) variables are properties of that function's Call object, which is created when the function is, what else, called - and destroyed when it returns. The only way to get the value to persist is to stick it somewhere, e.g., as a property of the Function object itself:
function foo1() {
alert(foo2.z);
}
function foo2(x) {
var z = x;
foo2.z = z;
}
foo2(5);
foo1();
http://comp20.eecs.tufts.edu/g/20/notes/js_tricks.php3
joh6nn
07-14-2002, 12:24 AM
thanks for seconding my opinion, adios. i thought that would work, but i wasn't sure.
adios
07-14-2002, 12:29 AM
Wasn't seconding anything, just trying to explain the principle involved, without using the word 'I' repeatedly...
RadarBob
07-15-2002, 01:24 AM
seems to me that if you define a variable outside of any function then it has global scope - well "file" scope is a more correct term IMHO. Thus one can access the variable w/out passing it as a parameter.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.