aaaaaaaaaaaa
aaaaaaaaaaaa

Reputation: 3700

JavaScript: Reference a functions local scope as an object

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.

Upvotes: 27

Views: 15349

Answers (5)

Dom Hastings
Dom Hastings

Reputation: 438

I know this is hugely late, and you're probably not even slightly interested any more, but I was interested in the feasibility of this too and you should be able to make a work around of some sort using:

(function(global) {
    var testVar = 1;

    global.scope = function(s) {
        return eval(s);
    }
})(this);

then running:

scope('testVar'); // 1

returns the variable from within the closure. Not particularly nice, but theoretically possible to wrap that in an object, perhaps using some validation and getters and setters if you needed?

Edit: Having re-read the question, I assume you'd want to access it without having to specify a function in the scope itself, so this probably isn't applicable. I'll leave this here anyway.

Upvotes: 11

T.J. Crowder
T.J. Crowder

Reputation: 1074268

No, there's no way to reference the variable object of the execution context of a function binding object of the variable environment of the execution context (that's what that thing is called [now; hence the strikethrough]; details in §10.3 of the specification). You can only access the limited view to it you get with arguments (which is very limited indeed).

Usually when I've wanted to do this, I've just put everything I wanted on an object and then used that (e.g., passed it into a function). Of course, any functions created within the context have access to everything in scope where they're created, as they "close over" the context; more: Closures are not complicated.

Upvotes: 16

SLaks
SLaks

Reputation: 887413

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)

Upvotes: 1

Ivo Sabev
Ivo Sabev

Reputation: 5240

What about something like this?

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

    test.foo();
</script>

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359816

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

Upvotes: -3

Related Questions