JavaScript: Reference a functions local scope as an object

Posted by eBusiness on Stack Overflow See other posts from Stack Overflow or by eBusiness
Published on 2010-04-08T13:49:13Z Indexed on 2010/04/08 13:53 UTC
Read the original article Hit count: 458

Filed under:
|
|

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 global 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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about scope