The improvement of variables and functions in JavaScript is actually quite commonly used in our daily projects, especially in large projects. Some variables will be added without realizing it. Sometimes your carelessness will lead to some unnecessary mistakes. Let's sort out your understanding of variables and function improvement in js while you have time.
First, look at a topic:
(a); // undefined var a = 1; (a); // 1 (b()); function b(){return 2;} // 2 (c) //Report an errorlet c = 4; (d()) // Report an errorvar d = function(){return 3;} // Report an error(function(){ var m = n = 1; })() (m) //Report an error(n) // 1
The above examples are very effective in improving javascript variables and functions...
1. Improvement of javascript variables
-The declaration of functions and variables will be promoted to the top of the function
-Variables can be declared after use, that is, variables can be used first and then declared
Knock the blackboard and point it out, it is the declaration, declaration, declaration of variables. When the variable or function has been initialized, it will not be promoted to the top of the function. The variable a above has been initialized, so the first console cannot get the value of variable a, which is undefined;
Note: The variable promotion will only be upgraded to the current scope
2. Improvement of javascript functions
Function b only declares a function method first, function c is the function expression cannot be improved, so in strict mode b=2; d reports an error
For m and n, here is the issue of javascript scope
First, what is the execution order of var m = n = 1? It is not the continuous assignment that most of us think of. The assignment of javascript is from right to left, but n=1; var m = n; very well, it is clear at a glance. The variable m=n in the scope of the function is n, n is a global variable. Finally, the output result in strict mode, n=1, m reports an error;
3. About the two keywords of es6 - let and const
The variable declared by let is only valid in the code block where the let command is located, and there is no variable promotion.
const declares a read-only constant, and once declared, the value of the constant cannot be changed. Once the declaration must be initialized, otherwise an error will be reported.
Let's have a very obvious comparison (perfectly reflecting the meaning that the let command is only valid in the code block):
{ let test = 2; var web = 'font'; } (test); // Report an error(web); // font
Go back to the above question again, c is an error
ES6 clearly stipulates that if there is let or const in the code block, the code block will form a closed scope for the variables declared by these commands from the beginning of the block. In the code block, using the variable PI before declaring it will report an error.
var PI = "a"; if(true){ (PI); // Report an error. Even if the PI is declared globally outside the function, an error will still be reported within the code block.const PI = "3.1415926"; }
The above is an understanding of variable promotion in javascript.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.