Basic knowledge of ES6

Keywords: Javascript REST github

demo on github

let&const

  • let does not support variable promotion

     console.log(a); // Report here
     let a = "a";
    
  • let cannot repeat declaration

     let a = "a";
     let a = "abc"; // Report errors
    
  • let supports block level scope

     if (true) {
       let a = "a";
     }
     console.log(a) // Report errors
    
     for (let i = 0; i < 5; i++) {
       setTimeout(() => {
         console.log(i); // 0 1 2 3 4
       });
     }
    
  • In addition to the let function, const cannot change the declared value, but it can add properties and change property values to the declared objects

     const PI = 3.14;
     PI = 3.141; // Report errors
    
     const obj = {
       name: 'Xiao Zhang'
     };
     obj.name = 'Xiaohong';
     obj.age = 25;
    

deconstruction

{
  // Array deconstruction assignment
  let [a, b, c] = [123, "abc", { name: "xiaohong" }];
  console.log(a, b, c); // 123 'abc' { name: 'xiaohong' }
}

{
  // Object deconstruction assignment
  let { name, age } = {
    name: "xiaohong",
    age: 25
  };

  console.log(name, age); // xiaohong 25
}

{
  // Custom name of value assigned to deconstruction
  let { name: myname, age: myage } = {
    name: "xiaohong",
    age: 25
  };

  console.log(myname, myage); // xiaohong 25
}

{
  // Default assignment. If assigned to age, the default value will be overwritten
  let { name, age = 19 } = {
    name: "xiaohong"
  };

  console.log(name, age); // xiaohong 19
}

{
  // Ellipsis assignment
  let [, , a] = [1, 2, 3];
  console.log(a); // 3
}

Expand operator

  • Use expansion operators in functions

     function test(a, b, c) {}
     let arr = [1, 2, 3];
     test(...arr);
  • Using expansion operators in functions in arrays

     let [a, b, ...c] = [1, 2, 3, 4, 5];
     console.log(a, b, c); // 1 2 [ 3, 4, 5 ]
    
     let arr1 = [1, 2, 3];
     let arr2 = [...arr1, 4, 5];
     console.log(arr2); // [ 1, 2, 3, 4, 5 ]
  • Class array variable to array

    function test(a, b, c) {
       console.log([...arguments]);
     }
     test(1, 2, 4); // [1 2 4]
    

Character string

  • Template string: before that, string splicing was completed with + sign. Now '` and S {} can replace string splicing

     let name = 'xiaohong', age = 25;
     let str = `My name is:${name},This year ${age}Year old`;
     console.log(str); // My name is xiaohong, 25 years old   
    
    {
     // Return value of custom template string
     let name = "xiaohong",
       age = 25;
     // ... rest as a parameter is only put at the end
     function desc(string, ...rest) {
       let str = "";
       for (let i = 0, len = rest.length; i < len; i++) {
         str += string[i] + rest[i];
       }
       str += string[string.length - 1];
       return str.toLocaleUpperCase();
     }
     let str = desc`My name is:${name},This year ${age}Year old`;
     console.log(str); // My name is XIAOHONG, 25 years old
    }
  • Judgment string starts with a string

     let str = "hello world!";
     console.log(str.startsWith('h')); // true
  • Judgment string ends with a string

     let str = "hello world!";
     console.log(str.endsWith('!')); // true
  • Determine whether the character creation contains a string

     let str = "hello world!";
     console.log(str.includes('hello')); // true
  • Duplicate string generation

     let str = "hello world!";
     console.log(str.repeat(3)); // hello world!hello world!hello world!   

Posted by robertf on Sat, 30 Nov 2019 16:24:38 -0800