[removed] Reference a functions local scope as an

2019-01-06 16:17发布

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.

6条回答
手持菜刀,她持情操
2楼-- · 2019-01-06 16:47

if you want to shift the scope of the function to any object you can use the underscore library with _.bind

In your case:

var fn = _.bind(function(){
    console.log(this == window)
},window)

fn() // true
查看更多
Explosion°爆炸
3楼-- · 2019-01-06 16:52

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-06 17:07

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.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-06 17:10

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)

查看更多
不美不萌又怎样
6楼-- · 2019-01-06 17:10

What about something like this?

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

    test.foo();
</script>
查看更多
The star\"
7楼-- · 2019-01-06 17:10

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

查看更多
登录 后发表回答