Indirection on ordinary vars.

T

Tim Streater

If I have an object, I can make a pointer to it. Such as:

mySelPtr = document.forms['myform'].myselect;

This can then be used as desired, e.g.:

var someVal = mySelPtr.options[0].value;

Now, is it possible to get a pointer to an ordinary var? If I have:

var someVar;

how can I get a pointer to someVar?
 
H

Henry

If I have an object, I can make a pointer to it. Such as:

It is potentially misleading to refer to this as a "pointer", as
'pointer' is usually used to refer to an address in memory where
something can be found and that is far too low-level to be part of the
javascript world.
mySelPtr = document.forms['myform'].myselect;

This can then be used as desired, e.g.:

var someVal = mySelPtr.options[0].value;

Now, is it possible to get a pointer to an ordinary var? If I have:

var someVar;

how can I get a pointer to someVar?

A variable is not an object.
 
T

Tim Streater

Henry said:
It is potentially misleading to refer to this as a "pointer", as
'pointer' is usually used to refer to an address in memory where
something can be found and that is far too low-level to be part of the
javascript world.

Then it's a reference. Consider my question suitably modified.
mySelPtr = document.forms['myform'].myselect;

This can then be used as desired, e.g.:

var someVal = mySelPtr.options[0].value;

Now, is it possible to get a pointer to an ordinary var? If I have:

var someVar;

how can I get a pointer to someVar?

A variable is not an object.

I guess I'm gonna have to create an array with one element so I can
treat it as an object.
 
T

Thomas 'PointedEars' Lahn

Tim said:
Henry said:
Now, is it possible to get a [reference] to an ordinary var? If I have:

var someVar;

how can I get a [reference] to someVar?
A variable is not an object.

I guess I'm gonna have to create an array with one element so I can
treat it as an object.

You can store the identifier of a variable as the value of the property
of an object; that does not require an Array object. However, with the
exception of global variables you cannot refer to a variable directly
because that would require a reference to the Variable Object of the
execution context which the variables of that context are properties of.
This is only possible for global variables because the Variable Object of
the global execution context is the Global Object which also happens to be
the Activation Object of that context.

var _global = this;
var answer = 42;

function foo()
{
window.alert(_global.answer);
}

foo();


PointedEars
 
T

Tim Streater

Thomas 'PointedEars' Lahn said:
Tim said:
Henry said:
On Mar 19, 2:05 pm, Tim Streater wrote:
Now, is it possible to get a [reference] to an ordinary var? If I have:

var someVar;

how can I get a [reference] to someVar?
A variable is not an object.

I guess I'm gonna have to create an array with one element so I can
treat it as an object.

You can store the identifier of a variable as the value of the property
of an object; that does not require an Array object. However, with the
exception of global variables you cannot refer to a variable directly
because that would require a reference to the Variable Object of the
execution context which the variables of that context are properties of.
This is only possible for global variables because the Variable Object of
the global execution context is the Global Object which also happens to be
the Activation Object of that context.

var _global = this;
var answer = 42;

function foo()
{
window.alert(_global.answer);
}

foo();

This is partially clear :)

What I need is the equivalent of:

var _global = this;
var answer = 42

function foo (ref)
{
alert (ref.value);
}

var myRef = _global.answer; // I know js doesn't do this :)

foo (myRef);


but perhaps this is not possible. Meanwhile I'll keep reading what you
wrote and using as a reference for searching the web.

Thanks -- tim
 
J

Joost Diepenmaat

Tim Streater said:
This is partially clear :)

What I need is the equivalent of:

var _global = this;
var answer = 42

function foo (ref)
{
alert (ref.value);
}

var myRef = _global.answer; // I know js doesn't do this :)

But that works.
foo (myRef);


but perhaps this is not possible. Meanwhile I'll keep reading what you
wrote and using as a reference for searching the web.

Just replace

alert (ref.value)

with

alert(ref);
 
T

Tim Streater

Joost Diepenmaat said:
But that works.


Just replace

alert (ref.value)

with

alert(ref);

<grin>

OK, I'm not being clear. Let me give an example from my actual
situation, where I'm trying to keep the code simple. In an onChange
function, I've got document objects to work on, and variables to manage.
In the following code fragment, I can get a reference to the forms
object and I have "invented" a ref() function that gives me a reference
to a variable, in order to illustrate what I would *like* to be able to
do. Clearly I can get round this with extra tests but then it all looks
very clumsy.

if (condition1)
{
docRef = document.forms["form1"].mySel_a; // maybe several of these
vblRef = ref (top.myVbl_a); // I would like to get a reference
}
else {
docRef = document.forms["form1"].mySel_b;
vblRef = ref (top.myVbl_b);
}

if (condition2)
{
docRef.disabled = true;
vblRef.value = 15; // some value
}
else {
docRef.disabled = false;
vblRef.value = 27; // some other value;
}


(condition1 and condition2 are unrelated)
 
J

Joost Diepenmaat

Tim Streater said:
<grin>

OK, I'm not being clear. Let me give an example from my actual
situation, where I'm trying to keep the code simple. In an onChange
function, I've got document objects to work on, and variables to manage.
In the following code fragment, I can get a reference to the forms
object and I have "invented" a ref() function that gives me a reference
to a variable, in order to illustrate what I would *like* to be able to
do. Clearly I can get round this with extra tests but then it all looks
very clumsy.

if (condition1)
{
docRef = document.forms["form1"].mySel_a; // maybe several of these
vblRef = ref (top.myVbl_a); // I would like to get a reference
}
else {
docRef = document.forms["form1"].mySel_b;
vblRef = ref (top.myVbl_b);
}

if (condition2)
{
docRef.disabled = true;
vblRef.value = 15; // some value
}
else {
docRef.disabled = false;
vblRef.value = 27; // some other value;
}


(condition1 and condition2 are unrelated)

Well, you can fake it, but you need both the object and the property
name:

function ref(obj, propname) {
return function(newv) {
if (arguments.length) obj[propname] = newv;
return obj[propname];
};
}

var vblRef = ref(top,'myVbl_b');

// get value

var val = vblRef();

// or set value

vblRef(27);

Joost,
 
T

Tim Streater

Joost Diepenmaat said:
Tim Streater said:
<grin>

OK, I'm not being clear. Let me give an example from my actual
situation, where I'm trying to keep the code simple. In an onChange
function, I've got document objects to work on, and variables to manage.
In the following code fragment, I can get a reference to the forms
object and I have "invented" a ref() function that gives me a reference
to a variable, in order to illustrate what I would *like* to be able to
do. Clearly I can get round this with extra tests but then it all looks
very clumsy.

if (condition1)
{
docRef = document.forms["form1"].mySel_a; // maybe several of these
vblRef = ref (top.myVbl_a); // I would like to get a reference
}
else {
docRef = document.forms["form1"].mySel_b;
vblRef = ref (top.myVbl_b);
}

if (condition2)
{
docRef.disabled = true;
vblRef.value = 15; // some value
}
else {
docRef.disabled = false;
vblRef.value = 27; // some other value;
}


(condition1 and condition2 are unrelated)

Well, you can fake it, but you need both the object and the property
name:

function ref(obj, propname) {
return function(newv) {
if (arguments.length) obj[propname] = newv;
return obj[propname];
};
}

var vblRef = ref(top,'myVbl_b');

// get value

var val = vblRef();

// or set value

vblRef(27);

Joost,

Joost,

I just tested this (in Safari 3.1):

vblRef = 'myVbl_a';
this[vblRef] = 22; // some value.

and it appeared to work.

Presumably I can do top[vblRef] also.

Thanks for your replies (and Thomas also). I think I learned something!
It also gave me something to google for, which is often the problem.

tim.
 

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