Scope
Scope refers to the scope of the code. According to the scope, variables can be divided into global variables and local variables; Scope can be divided into:
Global scope:
Refers to the scope of the global variable; Global variable refers to the variable declared outside the function through var, which can be used anywhere in js; However, the variables defined by var in the function cannot be used outside the function, and they are also local variables; If the page is not closed, the memory will always be occupied and cannot be deleted by delete.
<script> var num = 10; console.log(num); //10 delete num; </script> <script> console.log(num); //10 </script>
Local action Bureau:
Refers to the scope of action of local variables. Local variables refer to the variables declared in the function, which cannot be accessed outside the function. When the function is executed, the memory will be released.
<script> function f1() { var str = 'String in function'; console.log(str); }; f1(); //String in function console.log('Call local variables outside the function:' + str); //Uncaught ReferenceError: str is not defined at test.html:18 </script>
Implicit global variables:
It refers to variables that are not declared through keywords. The implicit global variables in the function can also be accessed outside. After the function is executed, the memory will not be released; It can be deleted by delete. If an implicit global variable is used before the variable appears, an error will be reported.
<script> function f1() { str = 'String in function'; // delete str;// Can be deleted console.log(str); }; f1(); //String in function console.log('Call local variables outside the function:' + str); //Calling a local variable outside a function: the string in the function console.log(b); //report errors function f1() { b = 5; }; f1(); console.log(b); //5. If the function is not called, an error will still be reported </script>
Block level scope:
It refers to a pair of {} inner regions. Variables declared in the block level scope can only be used in this block level scope, but there is no block level scope in es5js and there is a block level scope in es6. Therefore, variables defined in {} in JS can still be used outside, except functions.
<script> if (true) { var num = 10; }; console.log(num); //10 for (var i = 0; i < 3; i++) { var str = 'Hello'; }; console.log(str); //Hello </script>
Scope chain:
It means that the code of the current scope will find the variable first in the current scope. If it does not, it will find it up one level at a time. If there is no scope of level 0, it will report an error.
<script> //Level 0 scope: var num = 10; function f1() { //Level 1 Scope: var num = 9; function f2() { //Level 2 Scope: // var num = 8; function f3() { //Level 3 Scope: // var num = 7; console.log(num); //9 }; f3(); }; f2(); }; f1(); </script>
Pre resolution:
When the javascript resolves the variables of the current scope in advance, it also refers to the variables declared (before the javascript resolves the code of the current scope) in advance.
<script> console.log(num); //undefined, the pre parsing code is as follows: var num = 5; // Pre parsing code: var num; console.log(num); num = 5; f1(); var num = 10; function f1() { console.log(num); //undefined var num = 5; }; // Pre parsing code: var num; function f1() { var num; console.log(num); //undefined num = 5; }; f1(); num = 10; f1(); console.log(b); //5 console.log(c); //5 console.log(a); //report errors function f1() { var a = b = c = 5; console.log(a); // 5 console.log(b); // 5 console.log(c); // 5 }; //Pre parsing code: function f1() { var a; a = 5; b = 5; c = 5; console.log(a); console.log(b); console.log(c); }; f1(); console.log(b); console.log(c); console.log(a); console.log(f); //undefined var f = function() { var num = 5; console.log(num); }; // The parsing code is as follows: var f; console.log(f); f = function() { var num; num = 5; console.log(num); }; </script>
Tip: the pictures and other materials in this article come from the Internet. If there is infringement, please send an email to: 810665436@qq.com Contact the author to delete.
Author: Kuhai