Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Indirection on ordinary vars.

Reply
Thread Tools

Indirection on ordinary vars.

 
 
Tim Streater
Guest
Posts: n/a
 
      03-19-2008
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?
 
Reply With Quote
 
 
 
 
Henry
Guest
Posts: n/a
 
      03-19-2008
On Mar 19, 2:05*pm, Tim Streater wrote:
> 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.
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      03-19-2008
In article
<22900b6f-4562-4309-81ba->,
Henry <> wrote:

> On Mar 19, 2:05*pm, Tim Streater wrote:
> > 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.


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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-19-2008
Tim Streater wrote:
> Henry <> wrote:
>> 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();


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-20-2008
In article <>,
Thomas 'PointedEars' Lahn <> wrote:

> Tim Streater wrote:
> > Henry <> wrote:
> >> 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
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-20-2008
Tim Streater <> writes:

> 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);

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-20-2008
In article <>,
Joost Diepenmaat <> wrote:

> Tim Streater <> writes:
>
> > 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);


<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)
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-20-2008
Tim Streater <> writes:

> <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 Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-20-2008
In article <>,
Joost Diepenmaat <> wrote:

> Tim Streater <> writes:
>
> > <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Session varaible in an ordinary class RonL ASP .Net 1 04-02-2006 01:01 AM
what does ordinary telephone ports include FXS and FXO ŽStephen Hammond UK VOIP 2 02-13-2006 09:03 PM
voipfone question: can i use on my pc with ordinary headphone and mic? OM UK VOIP 2 09-21-2005 10:05 PM
Constructors are having a special signature, different from ordinary member functions ? Razvan Java 1 09-30-2004 06:12 PM
'superbit' DVD's versus ordinary DVD's... how much better? Mike DVD Video 37 07-13-2004 09:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57