Skip to content Skip to sidebar Skip to footer

Js Scope Access

[UPDATE] I get confirmation that I can not do that. All: I am pretty new to JS scope, say I have a function defined as : var scope = 'scope inside global'; function out(){ va

Solution 1:

I wonder how can I access "scope inside global" in scopeacess?

You cannot at all. You would need to use a different variable name for the local var scope so that you can access the global scope variable. Of course, you can also work around this by aliasing the global variable, or by passing its value around explicitly, so that it can be accessed through other means than the scope variable.

In your example, the names were probably only chosen to demonstrate what is going on (that closures are lexically not dynamically scoped), in actual scripts you wouldn't shadow the variables you need (or only by mistake).

Post a Comment for "Js Scope Access"