Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > accessing vars outside of object methods

Thread Tools

accessing vars outside of object methods

d d
Posts: n/a
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"]

Reply With Quote

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
Use self.vars in class.method(parameters, self.vars) caccolangrifata Python 18 07-22-2011 10:22 PM
How do I declare global vars or class vars in Python ? Linuxguy123 Python 7 02-20-2009 06:45 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
app vars and cache vars Jon ASP .Net 3 12-14-2004 08:52 PM