views:

98

answers:

4

When I call a function, a local scope is erected for that call. Is there any way to directly reference that scope as an object? Just like window is a reference for the global scope object.

Example:

function test(foo){
    var bar=1
    //Now, can I access the object containing foo, bar, arguments and anything
    //else within the local scope like this:
    magicIdentifier.bar
}

Alternately, does anyone have a complete list of what is in the local scope on top of custom variables?

Background: I'm trying to get down to a way of completely shifting to global scope from within a function call, the with statement is a joke, call works a little better, but it still breaks for anything declared in function scope but not in global scope, therefore I would declare these few cases in global scope, but that requires me to know what they are. The IE function execScript makes a complete shift, but that only solves the problem for IE.

Note: To anyone loading JavaScript dynamically, setTimeout(code,1) is a simple effective hack to achieve global scope, but it will not execute immediately.

A: 

Certain versions of Netscape had a magic property in the arguments object that did what you're looking for. (I can't remember what it was called)

SLaks
`arguments` is an variable containing all the arguments passed to the current function.
nickf
@nickf: I realize that. In certain versions of Netscape, it had a special proeprty that referred to the variable scope.
SLaks
@nickf: I misread it at first, too, but knowing SLaks I gave it a second reading and realized what he meant. :-)
T.J. Crowder
@SLaks, ahhh apologies, good sir.
nickf
I edited to clarify.
SLaks
Interesting, but I'm afraid what is available in "Certain versions of Netscape" just won't do in modern web development ;-)
eBusiness
It might still exist in Firefox. However, you're right; this doesn't actually help you.
SLaks
Nope, it's not there.
eBusiness
+2  A: 

No, there's no way to reference the variable object of the execution context of a function (that's what that thing is called; spec section 10.3), just the limited window to it you get with arguments (which is very limited indeed). If you can elaborate on what your end goal is, I may be able to help more, but I'm afraid I didn't quite understand what you were going for...

T.J. Crowder
Wow, those specs are really a pain to read. In any case, as far as I can tell, they seem to suggest that the only local variables apart from parameters and the defined locals are arguments and length. That should get me moving, thank you. I'm building a library, and the first function I'm making is a consistent version of setInterval, that is what this is for. Everything seems to grow so much more complicated when you have so supply a shiny abstractioned interface.
eBusiness
@eBusiness: Yes, yes it does. :-) Don't forget any named functions declared within the function, those are in-scope symbols as well.
T.J. Crowder
+1  A: 

You don't need a keyword to reference a variable in the local scope, because it's the scope you're in.

Matt Ball
yeah, I think he wants to iterate over them or something...
nickf
Iterate over every single variable that exists in the current scope? That doesn't sound Javascriptic (or whatever the Javascript analog of the word "Pythonic" is) to me.
Matt Ball
A: 

What about something like this?

<script type="text/javascript">
    var test =  {
        bar : 1,
        foo : function () {
            alert(this.bar);
        }
    }

    test.foo();
</script>
Ivo Sabev
If my aim was simply to write odd code that would have been a nice solution :-)
eBusiness
Lol. I don't see how it is odd since you just create a simple class structure.
Ivo Sabev