Earlier, read ruan yifeng god’s ES6 entry, thinking or output some things, decided to write down, the blog is not much, but still want to record, also when is later to see their own.

variable

1. let

Let has two main characteristics, namely, no variable promotion and temporary dead zones (if a block has a let or const in it, the block has a closed scope from the start. If you use these variables before the declaration, you will get an error.

 var temp = 123;
 if (true) {
     temp = 'abc';   // Refernce Error
     let temp;
 }
Copy the code

In the example above, the code block where the if statement is located has a temporary dead zone because of the existence of let. Therefore, temp= ‘ABC’ cannot be assigned to external temp, regardless of the external variable. Temp is also an undeclared variable. Therefore, an error will be reported.

Also, let is not allowed to be declared repeatedly in the same scope. Example:

 function func() {let a = 10;
     var a = 10;
 }
Copy the code

In the example above, the func function repeats the declaration of a, which results in an error.

Finally, the code block in which the LET resides forms a block-level scope. example

 function func() {let n = 5;
     if(true) {
         let n = 10;
     }
     console.log(n)    //5
 }
Copy the code

Because there is block-level scope in the code block where if is located, the JS engine will only look up the value of n along the scope chain, not down. So the value of n is 5 instead of 10.

2. const

The great thing about declaring a variable with const is that the variable must not change its value. Once declared, it must be initialized. By “immutable”, we essentially mean that the memory address to which a variable points cannot be changed. However, compound data (objects and arrays), which points to a memory address that holds a pointer, can only be guaranteed to be invariant.

const foo = {}; foo.prop = 123; // success foo = {}; / / an errorCopy the code

In the example above, the variable foo is an object, so all that is held in memory is the address of the variable, as long as the address stays the same. So assigning an attribute to foo will work because the address of the foo variable itself is not changed.

** Note that global variables declared by the let, const, and class commands are not attributes of the top-level object.

3. Structure assignment of variables

The schema assignment of variables is mainly for objects and arrays, with key as the lookup object, and undefined as the lookup object. Example:

 let [x, y, z] = [1, 2, 3];
 let [head, ...tail] = [1, 2, 3]            // head = 1, tail = [2, 3]
 let [a, b, c, d, e] = 'hello'             //a = 'h', b = 'e', c = 'l', d = 'l', e = 'o'
 let {foo, bar} = {foo: 'a', bar: 'b'}    //foo = 'a', bar = 'b'
 let {foo: baz, bar: last} = {foo: 'a'}   //baz = 'a', last = undefined
Copy the code

The main application scenarios are as follows: 1. Exchange variable values:

   let x = 1;
   let y = 2;
   [x, y] = [y, x];    //x = 2, y = 1
Copy the code

2. When multiple values are returned from a function, the value is convenient

  let example = () => [1, 2, 3];
  let [a, b, c] = example();   // a = 1, b = 2, c = 3
Copy the code

3. Extract JSON data

  let jsonData = {id: 42, status: 'ok'};
  let {id, status: number} = jsonData;    //  id = 42, number = 'ok'
Copy the code

The data type

Symbol

A new data type in ES6 that represents values that are never duplicated. Example:

  let s1 = Symbol(33);
  let s2 = Symbol(33);
  console.log(s1 == s2)   //false
  
  let s3 = 2;
  let s4 = 2;
  console.log(s3 == s4)   //true
Copy the code

As you can see from the example, S1 and S2 are not equal. Application scenario: When using an object provided by others, but you want to add a new method to the object, the new method may conflict with the existing method, you can use Symbol to ensure that there is no conflict with other attributes.

The data structure

Es6 adds different data structures for arrays and objects. Here I only briefly introduce the features and application scenarios.

1. Set

New data structure, like an array, but the values in a set are not duplicated. Array = [… new Set(array)]

2. WeakSet

Members can only be objects, and the garbage collection mechanism does not consider the reference to objects in weakSet.

3. Map

The essence is the same as an object, but a key can be any type of value (including an object), and a key in an object is a string.

4. WeakMap

Only objects are accepted as key names, and the object to which the key names refer is not counted in the garbage collection mechanism. Application scenario: WeakMap can be used when you want to add data to a DOM element. This prevents the DOM element from being deleted and having a reference to it and not being able to reclaim memory.