Indirection on ordinary vars.

Discussion in 'Javascript' started by Tim Streater, Mar 19, 2008.

  1. Tim Streater

    Tim Streater Guest

    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?
     
    Tim Streater, Mar 19, 2008
    #1
    1. Advertising

  2. Tim Streater

    Henry Guest

    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.
     
    Henry, Mar 19, 2008
    #2
    1. Advertising

  3. Tim Streater

    Tim Streater Guest

    In article
    <>,
    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.
     
    Tim Streater, Mar 19, 2008
    #3
  4. 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
     
    Thomas 'PointedEars' Lahn, Mar 19, 2008
    #4
  5. Tim Streater

    Tim Streater Guest

    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
     
    Tim Streater, Mar 20, 2008
    #5
  6. 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/
     
    Joost Diepenmaat, Mar 20, 2008
    #6
  7. Tim Streater

    Tim Streater Guest

    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)
     
    Tim Streater, Mar 20, 2008
    #7
  8. 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/
     
    Joost Diepenmaat, Mar 20, 2008
    #8
  9. Tim Streater

    Tim Streater Guest

    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.
     
    Tim Streater, Mar 20, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    397
  2. RonL
    Replies:
    1
    Views:
    1,468
  3. Razvan
    Replies:
    1
    Views:
    367
    Tor Iver Wilhelmsen
    Sep 30, 2004
  4. Linuxguy123
    Replies:
    7
    Views:
    697
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    397
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page