JS Scope Understanding function () {} var

Source: Internet
Author: User

The following conditions have been encountered during the coding process:

1 var function () {  2     var c=4; 3     test2 (); 4 }; 5 6 var function () {  7    Console.log (c); 8 }; 9     ten test1 ();

Think about what the 7 guild Conasole out. 4, huh? It is actually a C is not defined.

And when the code is as follows,

  1  var  test1 = function   () {  var  c=4 3  var  test2 = function   () {   Console.log (c);   };    7   test 2 ();   };   9  10  11  test1 (); 

Or the code is as follows

1 var function () {  2     c=4 3    test2 (); 4 }; 5 6 var function () {  7    Console.log (c); 8 }; 9     ten test1 ();

The console out is 4.

What is this for?

First, let us understand two points of knowledge:

1, JS scope: Scope of the scope of action is divided into global scope and local scope.

A, global scope: from the position where the variable is defined to the end of the source file. Where A is a global variable, the scope is global, so the 4 and 6 can be console out of 3.

1 varA = 3;2 3 varTest3 =function(){4 Console.log (a);5     varTest4 =function (){6 Console.log (a);7     };8 test4 ();9 };Ten  OneTest3 ();

B, local scope. within the scope of this function. In layman's terms, a local variable can only be used inside a function that defines it, but not within other functions.

1 varTest3 =function(){2 Console.log (a);3     varTest4 =function (){4         varA = 3;5 6 Console.log (a);7     };8 test4 ();9 };Ten  OneTest3 ();

We put var a = 3 in the previous code, and move to line fourth, there is an error in the 2 row of the console where a does not have a definition, because a is defined in the TEST4 function, so the external function test3 cannot access the test4 defined in the package.

There is another concept: the intrinsic function can access the variables of the external function, and the external cannot access the variables of the intrinsic function.

1 varTest3 =function(){2     varA = 3;3 Console.log (a);4     varTest4 =function (){5 Console.log (a);6     };7 test4 ();8 };9 TenTest3 ();

We re-put var a = 3; Move to Line 2nd, then the console with 3 rows and 5 rows can be played normally. Description Test4 can access the defined variable a of the external test3.

2, the difference between Var and not var. var declares a local variable, and a global variable is declared without var.

OK, now go back to the beginning of the problem, and then paste the code again, easy to look at.

var function  2     var c=43     45  var function  7     89     test1 ();

This is the console does not come out, what is the reason?

There are a total of two functions, test 1 and Test 2, each with a local scope, where var c = 4 is in Test 1, not in Test 2, so test 2 cannot call the variable C in Test 1. So you're going to say I called Test 2 again on the third line? That is because Test2 () is a pointer to the test 2 function below, but the pointer can only invoke the memory address of the object, you can use its value, but you cannot change or invoke any variables in its internal implementation. Simply put, you can only call the value of this function, but you cannot change it, and you cannot affect it.

And when there is no Var declaration, that is the third paragraph of code:

var function  2     c=4 3     45  varfunction78       9     test1 ();

Although there is no word var, but it does not mean that it does not declare, but this time declared a global variable, his scope is global. So the test 2 function below can call C = 4.

Or code like the second paragraph:

var function  2     var c=4 3     varfunction4         56           7     89 test1 ();

In the same vein, if you don't look at Test 1,

1 var c=4; 2 var function () {3    Console.log (c); 4 }; 5       6

C is the global variable, test 2 of course can use C ~ Plus Test 1, that is, for Test two said the test 1 function declared within the variable is a global variable, in the entire test 1 can be called.

Finish.

JS Scope Understanding function () {} var

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.