I am just curious if there is such a thing as a 'local scope object' in JavaScript. If you invoke a function, it has a context (this
), which is the object it has been called on (function f() {return this;}; obj.f = f; obj.f(); //returns obj;
), and a scope, which is created on every function call. The scope is used to define local variables as in the following example:
var globalScopeVar = 1;
(function() {
var localScopeVar = 2;
})();
In both scopes this
refers to the global context (typically, window
), since the function has not been called on any object. What I am interested in, though, is the 'scope object', i.e, the object where variables within a scope are defined on. For the global scope this is typically window
, just like the global context:
window.globalScopeVar; // 1
However, what is the 'scope object' in the local scope of a function call? Does it even exist or is it accessible? Is there any way to access an object where localScopeVar
is defined on?
(function() {
var localScopeVar = 2;
localScope.localScopeVar; // 2
})();
What is localScope
in this example?
Local scope is a characteristic of variables that makes them local (i.e., the variable name is only bound to its value within a scope which is not the global scope).
Local (or function) scope is the code block or body of any Python function or lambda expression. This Python scope contains the names that you define inside the function. These names will only be visible from the code of the function.
A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language − Inside a function or a block which is called local variables.
A global variable has global scope. A global variable is accessible from anywhere in the code. Local Scope — Local scope contains things defined inside code blocks. A local variable has local scope. A local variable is only accessible where it's declared.
That localScope
is a Lexical Environment.
As an answer to your question if is it accessible
, ECMAScript Language Specification says the following:
It is impossible for an ECMAScript program to directly access or manipulate such values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With