accessing vars outside of object methods

D

d d

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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top