current position:Home>Winter Vacation JavaScript Tutorial Chapter 1 Variable Promotion

Winter Vacation JavaScript Tutorial Chapter 1 Variable Promotion

2023-01-25 10:50:20small fish programmer

In fact, the previous posts here have talked about a lot of js prefixes, so I won’t talk about the prologue. In a word, when practicing code, no matter who you are, you can’t do without typing and writing, although some people may have cv problems in their work, I’m asking you a question, if it’s not a boss, what’s the use of his cv, as the saying goes, there are tens of thousands of Baidu articles, just to find the answer, study in the early stage, and in the later stage is for work needs, after all, you will encounterAll kinds of weird mistakes, you are not artificial AI, so why not go to Baidu to find the exclusive answer cv to solve the work needs, what are you doing? You have learned it, and you will do the same.

Let's get back to business. We said before that js and node are two different things, one front end and one back end. At the same time, the first chapter we are talking about today is variable promotion

The working principle of JavaScript is to compile and then execute. During the compilation process, the interpreter will "move" all declarations to where top of scope, while assignment or other logic is left in place,This is variable hoisting.

2. Function priority

During hoisting, function declarations are hoisted first, followed by variables.Only function declarations ( function a(){} ) are hoisted, function expressions ( var a = function b(){} ) are not hoisted.If there are identical declarations, the same variable declarations will be ignored; for the same function declarations, the later ones will override the former ones.

For example

foo(); // 1var foo;function foo(){console. log(1);}foo = function(){console. log(2);}

will be understood as

function foo(){console. log(1);}foo(); // 1foo = function(){console. log(2);}

copyright notice
author[small fish programmer],Please bring the original link to reprint, thank you.

Random recommended