よく使うES 6/7の新特性

9325 ワード

ES 6/7の新特性
  • 文字列テンプレート
  • let
  • const
  • 矢印関数(デフォルトパラメータ)
  •   const action = (num = 10)=> console.log(num)
      action()
      action(300)
  • オブジェクト初期化簡略化
  •   var name = "dukai"
      var age = 28
      var person = {name, age}
  • オブジェクト方法略記
  • Object.assign()この方法は、レプリカ
  • を実現する.
  • 解構成
  •     //  
        const people = {
            name: 'dukai',
            age: 28
        }
        const { name, age } = people
        console.log(`${name}: ${age}`)
        //  
        const color = ['red', 'blue']
        const [first, second] = color
        console.log(first, second) 
  • Spread Operator展開演算子
  •     //   
        const color = ['red', 'yellow']
        const colorful = [...color, 'green', 'pink']
        console.log(colorful) //[red, yellow, green, pink]
    
        //   
        const child = { fist: 'a', second: 'b'}
        const parent = { ...child, third: 'c' }
        console.log(parent)
    
        //         
        //   
        const number = [1,2,3,4,5]
        const [one, ...part] = number
        console.log(one)  //1
        console.log(part) //2,3,4,5
        //   
        const user = {
            username: 'dk',
            gender: 'male',
            age: 28,
            address: 'Beijing'
        }
        const { username, ...part } = user
        console.log(part) //{ "gender": "male,  "age": 19,"address": "Beijing"}
    
        //      ,    ,**    **
        const first = {
            a: 1,
            b: 2,
            c: 6,
        }
        const second = {
            c: 3,
            d: 4
        }
        const total = { ...first, ...second }
        console.log(total) // { a: 1, b: 2, c: 3, d: 4 }
  • import導入モジュール、export導出モジュール
  •   1.   export default people   ,   import people   (     )
    
      2.export defaultexport3.   export name  ,  import { name }  (       )
    
      4.export default people,     export name    export age ,     import people, { name, age } 
    
      5.         n   export       ,           ,    import * as example
  • Promise
  •     setTimeout(function() {
          console.log(1)
        }, 0);
    
        new Promise(function executor(resolve) {
          console.log(2);
          for( var i=0 ; i<10000 ; i++ ) {
            i == 9999 && resolve();
          }
          console.log(3);
        }).then(function() {
          console.log(4);
        });
    
        console.log(5);
  • Generators–ジェネレータは、1つのサブジェネレータを返すことができる関数
  • です.
  • はちょっと難しいです.asyncに来てPromiseオブジェクトに戻りました.
      async function reqApis(name) {
        var user = await req.getUser(name);
        var articles = await req.getArticles(user.id);
        // it is real article list here, but if returned it'll be a Promise 
        return articles;
      }
      // Becuase of the 'async' the result is Promise,
      // so we should use it like this
      reqApis('dukai').then((articles)=> {
        renderArticles(articles)
      })
    
  • クラスの使用について
  •   class Person {
        constructor(type) {
            this.type = type;
        }
        says(say) {
            console.log(`${this.type} says ${say}`);
        }
      }
      let person = new Person("person");
      person.says('hello'); 
    
      class Man extends Person {
        constructor(type, age) {
          super(type);
          this.type = type;
          this.age = age
        }
    
        says(say) {
          console.log(`${this.type} says ${say},  I'm ${this.age} years old.`);
        }
      }
      let jack = new Man("dukai", 28);
      jack.says('hello'); 
  • 対象の深度コピーの奇術はES 6/7に専属されていませんが、属性には方法や循環参照があります.古い実用サイクルについて再帰してください.