What is scope in javascript?

I do not understand what a scope is. I read somewhere that scope is a way to access varible. But itโ€™s hard for me to find a case where a variable is reachable. Access to all variables inside the function can be obtained through the context of the global or activation / variable object or through closure. Here is a code snippet to demonstrate what I mean:

var global_var = 7; var f = (function() { var closure_var = 5; return function() { var local_var = 3; alert(local_var); // alerts 3 - visible through context as Activation Object property alert(closure_var); // alerts 5 - visible through closure alert(global_var); // alerts 7 - visible through context as Global Object property alert(this.global_var); // alerts 7 - visible through context as Global Object property } })(); f(); 

So what is scope? Here is an excerpt from here and my comments:

 // a globally-scoped variable var a=1; // global scope function one(){ alert(a); } // no, it accessible as global object context // local scope function two(a){ alert(a); } // no, it accessible as activation object context 

EDIT:

Thanks to everyone. I assume that I will need to look at the scope from the point of a variable and function.

+4
source share
5 answers

Area - The area in which the variable is active. As a function or page.

global_var and f are global for the page because they are outside all functions, therefore they are available for all functions.

local_var is local to f (), so it is not available outside of f ().

+1
source

Iโ€™m going to suggest that you know what a field of computer science is. If not, read the wiki page.

In javascript, every function has a scope. In addition, there is a global scope (when a variable is defined outside of any function in the global scope or in a function if var not specified in the definition). Areas of hierarchy. If you have a function F and a function G inside F, when you try to access a variable in G, it checks to see if the variable is defined in area G. If not, it will try to use the F-area. If not, he will try in a global area.

 // global scope var a = 1; function F() { var a = 2; function G() { var a = 3; // here, a is 3 } // here, a is 2 } // here, a is 1 

All 3 variables are different (because they were defined with var ), and you can change them in a global scope, in F or in G.

 // global scope var a = 1; function F() { a = 2; function G() { a = 3; // here, a is 3 } // G is called, a is changed.. G(); // here, a is 3 } // F is called, a is changed.. F(); // here, a is 3 

All 3 variables are actually one, available anywhere (because it is in a global scope). Change will affect all 3 areas.

 // global scope var a = 1; function F() { a = 2; function G() { var a = 3; // here, a is 3 } // here, a is 2 } // here, a is 1 

Since a was defined in G , inside it it is distinct and isolated from the outside. Meaning, nothing outside G can access or change the variable a , which is inside. They will only see a (if defined) from the global area. Also, due to the behavior of the function region, the function G exists only inside F ; you cannot call it from the outside.

Look at the areas like containers. They can be nested. You cannot look inside, but they may look outside.

+1
source

JavaScript has two areas: global and local. A variable declared outside the function definition is a global variable, and its value is available and modified throughout your program. A variable declared inside a function definition is local. It is created and destroyed every time the function is executed, and no code outside the function can access it.

Taken from the MSDN page about variable scope. READ THIS!

0
source

The area in which you can directly access any variable is called its local area. Any variable declared outside of all functions is in the global scope. Thus, a very simple local area is the area to which the variable can be directly accessed.

But a local scope does not mean that you cannot access any variable outside its scope. Here closure goes into the picture, and here is one simple example of closure:

 function a () { var q = 10; return function () { alert(q++) } } var b = a(); // returns a function for which var q is local 

Now every time you call b on b() , var q will increment.

JavaScript has one respect between function and variable declarations.

You can access the function in any area in which it is declared, but access to variables is possible only after it is declared. Here is an example:

 function ab() { t(); // alert 1 alert(a1); // alert undefined var a1 = 1000; // Now declare a1 alert(a1); // alert 1000 function t() { // define function alert(1); } } 
0
source

I think I found the answer here :

When creating the execution context, several events occur in a specific order.
...
Next, the execution context is assigned SCOPE. An area consists of a list (or chain) of objects. Each function object has an internal property [[scope]] (which we will discuss in more detail), which also consists of a list (or chain) of objects. The scope assigned to the function call execution context consists of a list referenced by the [[scope]] property of the corresponding functional object, with an activation object added at the beginning of the chain (or at the top of the list).

0
source

Source: https://habr.com/ru/post/1498440/


All Articles