Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local scope object

Tags:

javascript

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?

like image 314
Helge Avatar asked Feb 09 '15 11:02

Helge


People also ask

What is a local scope?

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

What is local scope in Python?

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.

What is local scope in C?

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.

What is local scope and global scope?

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.


1 Answers

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.

like image 68
a--m Avatar answered Oct 23 '22 02:10

a--m