“Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay competition”
review
In the last article, we mainly said js function, involving the concept and use of function, as well as function parameters and call and other aspects of the content, function is the most representative function in JS, but also the future is very common technical points. The content of the article is very basic, if you want to know more in-depth advice to read the little Red book and other books.
Scope of this article, we ask the js and parsing, for normal thinking, you must feel this part will be confused, especially for content parsing, but it doesn’t matter, you can try to learn more about partly due to the development course of js will find that the development of language is not open around itself in the present environment and many factors such as time.
scope
Scope: The scope and effect of a code name (variable) to improve program reliability and reduce naming conflicts.
Js (before ES6) is generally divided into: global scope, local scope
-
Global scope: the entire script tag, or a single JS file
-
Local scope: within a function, or within a block
Scope of variables: Variables can also be divided into two types depending on their scope
- Global variables: declared under global effects
Note: global variables are destroyed only when the browser is closed, which consumes memory resources
- Local variable: declared under local (a function or method) effects
Note: local variables will be destroyed automatically after the execution of our program to save memory resources
However, variables that are not directly assigned to a function or method are also global variables. But function parameters are local variables.
There is another important point in scope: scope chain (this review will focus on es6). For the moment, we need to know that scope chain is the process of chain search, which means that if the internal function or method does not find the corresponding value, the search layer by layer.
// scope chain, Function name1 () {var num = 1 console.log('111', num) // 1 function name2 () {console.log('222', num) num) // 1 } name2() } name1()Copy the code
The name2 function does not declare num, but the output needs num, so it will search up and return if it finds num, and error if it does not find num. This also confirms the fact that internal functions can access external functions.
Preliminary analysis
Pre-parsing: The JS engine pushes all the var and function functions in the JS to the front of the current scope and executes them from top to bottom in the order the code is written
Preparsing is divided into variable preparsing (variable promotion) and function preparsing (function promotion)
-
Variable promotion: To promote all variable declarations to the front of the current scope without promoting assignments
-
Function promotion: To promote all function declarations to the front of the current scope without calling the function
For example
Case 1:
Function name() {console.log(name) // undefined var name = 'Andy'} name() function name() { var name console.log(name) // undefined name = 'andy' } name()Copy the code
This is because the variable declared by var causes the variable to be promoted, and both var and function declare variables and functions to be promoted to the front of the current scope
Case 2:
Function fn() {console.log(11)} function fn() {console.log(11)} function fn() {console.log(11)} fn() {console.log(11)}Copy the code
Case 3:
Function: Fun () // fun is not a function var fun = function() {console.log(11)} var fun() fun = function() { console.log(11) }Copy the code
Well, that’s it for today, scoping and pre-parsing is one of the most iconic features of javascript. You can print it out in the editor and see how it works. You can also use debugging tools to see how it works. The content is fairly introductory, just to help you understand or review. Any questions please leave a message at any time ~