accessing vars outside of object methods

Discussion in 'Javascript' started by d d, Jul 23, 2007.

  1. d d

    d d Guest

    I've got some objects that have methods and I'm wondering if it's
    possible for those methods to access the variables defined outside of
    them, but still inside the object, without addressing them with "this.".
    I can access them fine by addressing them with "this." but I'd like to
    access them without that if possible.

    This none-object-based structure would work fine but I'm NOT using
    anything like this:

    function outerfunc(){
    var outervar=" world";
    function innerfunc(){alert("hello"+outervar);}
    }
    outerfunc(); //alerts hello world

    The object based code I have (here below) also works fine, but only
    because I'm accessing the variable via this. and that's what I'm trying
    to avoid.

    outerobj={outervar:" world",
    innermethod:function(){alert("hello"+this.outervar);}
    }
    outerobj.innermethod(); //alerts hello world only if "this." used

    I'm just thinking about code size. If I end up needing to use "this."
    200 times, then there's 1000 bytes that I'd rather not have in my code.
    I could declare var t=this; at the start of each method, so I can access
    vars with just "t." but it's not guaranteed to pay off in each function
    due to the overhead of defining t.

    I probably won't be allowed to modify any object prototypes, so if any
    solutions involve that, then they'll be no good for me.

    I'm pretty sure it's not possible to do what I want, but with the gurus
    that frequent this place like Richard and Rob and Douglas and Martin and
    the Doc (OK I better stop now in case I leave anyone out), I'm
    constantly surprised :)

    I still can't get over that I was still using eval when I could just do
    window["some string"] :)

    ~dd
    d d, Jul 23, 2007
    #1
    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:
    380
  2. Linuxguy123
    Replies:
    7
    Views:
    669
    Paddy O'Loughlin
    Feb 20, 2009
  3. caccolangrifata
    Replies:
    18
    Views:
    385
    Chris Torek
    Jul 22, 2011
  4. Kenneth McDonald
    Replies:
    5
    Views:
    301
    Kenneth McDonald
    Sep 26, 2008
  5. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    274
    Victor Bazarov
    Feb 7, 2012
Loading...

Share This Page