By Lydia Hallie

Translator: Front-end wisdom

Source: dev

Like it and see. Make it a habit

In this paper,GitHub Github.com/qq449245884…Has included more categories of previous articles, as well as a lot of my documentation and tutorial material. Welcome Star and Perfect, you can refer to the examination points for review in the interview, I hope we can have something together.

If you’re new to JS, you may encounter some strange behavior. Some variables are randomly undefined, ReferenceErrors exceptions are thrown, and so on. Promotion is often interpreted as putting variables and functions at the top of the scope, but this is not actually the case, although the behavior appears to be so.

When the JS engine gets our code, the first thing it does is set up memory for the data in the code. No code has been executed yet, just preparing everything to be executed. Function declarations and variables are stored differently. Functions are stored as references to functions.

1. Functions are stored as references to functions (Functions are stored with a reference to the entire function)

Using variables is different. ES6 introduces two new keywords to declare variables: let and const. Variables declared with the let or const keyword hold the state of uninitialized.

2. UseletorconstThe state of the variable declared by the keyword isuninitialized

The default value for variable stores declared using the var keyword is undefined.

Now that the creation phase is complete, we can execute the code. Let’s see what happens if three console.log statements are executed at the top before declaring a function or any variable.

Since functions are stored in references to the entire function code, we can call them before creating their lines.

4. During the execution phase, we can call functions before declaring them because we reference the whole function in memoryexecution phase, we can invoke a function before we declared them, since we a reference to the entire function in memory)

When we reference a variable declared by the var keyword before the variable declaration, it only returns the default value it was stored with :undefined. However, this can sometimes lead to “unexpected” behavior. In most cases, we may have inadvertently rereferenced it (you probably don’t want it to be undefined)

5. When we reference a variable before it is declaredvarKeyword when a variable is declared, it will only return the default value when it is stored:undefined

To prevent accidental references to undefined variables, just as we can use the var keyword, we raise a ReferenceError whenever we try to access an uninitialized variable. The ‘zones’ before they are actually declared are called ** temporary dead zones **: variables cannot be referenced before their initialization (this also includes ES6 class’)

6. Every time we try to access an uninitialized variable, we raise a ReferenceError. The “zone” before they are actually declared is called a temporary dead zone **

When the engine passes the line where we actually declared the variable, the value in memory is overwritten by the value we actually declared.

conclusion

  • Before executing code, functions and variables are stored in memory for execution context, which is called promotion.

  • The function store is a reference to the entire function. The default value of variables declared with the var keyword is undefined, and the default value of variables declared with the let and const keyword is uninitialized.

Hope you found this article helpful. Thank you for reading it.


Original text: dev. To/lydiahallie…

The bugs that may exist after code deployment cannot be known in real time. In order to solve these bugs, I spent a lot of time on log debugging. Incidentally, I recommend a good BUG monitoring tool for youFundebug.


communication

Dry goods series of articles summarized as follows, feel good point Star, welcome to add groups to learn from each other.

Github.com/qq449245884…

I am Xiaozhi, the author of the public account “Big Move the world”, and a lover of front-end technology. I will often share what I have learned to see, in the way of progress, mutual encouragement!

Pay attention to the public number, background welfare, you can see the welfare, you know.