Reading notes: In-depth understanding of ES6 (1)

Opening words: This is the first one of the reading notes about "In-depth Understanding of ES6", mainly for reading the first chapter. This note basically only records ideas and core knowledge points. For specific content, you can refer to the code and detailed explanations in the book, in order to achieve the function of outline and easy review in the later period.

 

 

Chapter 1 Block-level scope binding

  1. Var declaration and variable hoisting mechanism.

  In this section, I mainly talked about some small defects of var to declare variables as a frequently used keyword in the original ES5, including some problems caused by the variable that it declares can be read in other locations. Because the variable declared by var usually has a hoisting mechanism, in the code block, even if the variable is not declared yet, the execution flow can still know that the variable already exists, but the value is undefined.

  2. Block-level declaration.

  In this section, it mainly describes some rules of let and const keywords to declare variables, as well as the difference with var declared variables. Variables declared by let can only be read within the declared block (curly braces in the code), and variables declared by let cannot be declared repeatedly, otherwise an error will be reported. The variable value declared by const is constant, which means that after it declares a variable, the value of this variable cannot be modified. There is also the concept of "temporary dead zone". This can be understood as when the use of var is changed to let or const, the original part where hoisting can be used cannot be improved now, so it becomes " Temporary dead zone".

  3. Block scope binding in the loop.

  In this section, starting with a for loop function, it is first described that if you follow the usual thinking and use the condition of var i = 0; i <10; i ++, the printed value is actually not normal after it is inserted into the array at the end Thinking of 0,1,...,9, but 10 10, this is a loophole in the original JS, or a bad design, if you use the original specification, you must use the immediate execution function to Finally, the expected result of storing the array into 0, 1, ..., 9 is realized. Then, in the ES6 specification, this defect is fixed, that is, if the let statement or the const statement is used, then the value of i will be a new binding every time it loops, instead of using the original binding. In this way, the new value is added to the array, which solves a defect of the original var loop declaration.

  4. Global block scope binding.

  In this section, I mainly talked about the relationship between var let const and window. When using var to declare a global variable, if the variable is already a property on the window object, this declaration is still valid, but it is easy to conflict with the original built-in object of the window; now, if you use let or const to declare one In the case of global variables, then the original properties on the window object and at this time will not overwrite each other, which is equivalent to the declared variable more accurate.

Guess you like

Origin blog.csdn.net/ZxxSteven/article/details/100145607