ES6 Review-let & const

1、ES5 vs ES6

    ES stands for ECMAScript. ECMAScript is a specification of Js scripting language.

    ES5 is the fifth edition of ECMAScript, which was completed in 2009, and the specifications have been implemented in current browsers.

    ES6 is the sixth edition of ECMAScript, which was completed in 2015. This standard has been implemented in most browsers.

    ES6 refers to the next generation JS language standard, including ES2015 to ES2020, etc. ES2015 is equivalent to ES6 by default. ES5 refers to the previous generation language standard. ES2015 can be understood as the time boundary between ES5 and ES6.

 

2、Babel

    Babel is a transcoder. ES6 is implemented on most browsers. For platforms that do not implement ES6, you can use Babel to convert ES6 to ES5 for execution.

 

3、let && const

 

  • let

      let and var are similar, the role is to declare a variable.

          (1) Different scope

      Let is a block scope, and the scope only takes effect in its code block area; var defines global variables, which can still be called out of the code block.

 

for(let i=0; i<1; i++) {
    var a = 12;
    let b = i;
}

a    // 12
b    // Uncaught ReferenceError: b is not defined

 

    (2) Variable promotion

      Variable promotion means that the variable declaration is promoted to the top of the code, that is, the variable can be called before the variable is declared.

      The let  command has no variable promotion. If you call the variable defined by let before the declaration, you will get an error.

      The var command can be called at will, as long as the code is declared.

console.log(a);
var a = 234;    // undefined

console.log(a);
let a = 234;     // Error: Cannot access 'a' before initialization

    (3) Temporary dead zone

      ES6 stipulates that if there are let and  const commands in the block in the code block,  the variables declared by these blocks in the block form a closed scope from the beginning .

letThe variable is not available until the variable is declared       using the command.

// 死区 开始
hunter = 'abc';  // ReferenceError: Cannot access 'hunter' before initialization
console.log(hunter);  // ReferenceError: Cannot access 'hunter' before initialization

let hunter;
// 死区 结束
console.log(hunter);  // undefined

hunter = "QQQ";
console.log(hunter);  // QQQ

    (4) Repeated statement

      The let command is different from var. The let command does not allow repeated declarations

var a = 1; 
var a = 2;
a;    // 2

let a = 1;
let a = 2;    //SyntaxError: Identifier 'a' has already been declared

  

 

  • const

    const is used to declare constants. Constants need to be initialized when they are declared. After initial assignment, the value of the constant will not change.

const hunterTest = 123;
hunterTest;    // 123

hunterTest = 666;    // TypeError: Assignment to constant variable.

     const, in my understanding, is to save the memory address pointed to by the variable, and has limitations for simple data structures such as Number, String, and Boolean. However, for variables of a compound data structure, the variables remain unchanged, and the values ​​inside the variables can change.

const aaa = 123;
aaa = 345;    // TypeError: Assignment to constant variable.


const aaaa = {
    name: "YuHang"
}
aaaa["GG"] = 234    // {name: "YuHang", GG: 234}


var bbb = { name: "Lisa"};
aaaa = bbb;    // TypeError: Assignment to constant variable.

  

Guess you like

Origin www.cnblogs.com/Lyh1997/p/12746398.html