xinggevip

Es2015 Sybol、generator
index.html<!DOCTYPE html> <html lang="en&qu...
扫描右侧二维码阅读全文
18
2019/06

Es2015 Sybol、generator

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <title>es6test</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but es6test doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <script type="module">
      // 以前的数据类型只有String、number、Array、Boolean、Object、undefined、function
      // 新增数据类型Symbol
      // 特点:不能new出来,可用于存放key,私有数据
      let aa = Symbol("aaa");
      console.log(typeof(aa)); // symbol
      console.log(aa);         // Symbol(aaa)

      let json1 = {
        a:"apple",
        b:"banana0",
        [aa]:"Tom"
      }
      console.log(json1[aa]); //String
      console.log(typeof(json1[aa])); // Tom

      // Symbol作为key在for in 中循环不出来
      for(let key in json1){
        console.log(key); // a b
      }

      // genarator函数 生成器 解决异步,深度嵌套 async
      // 定义
      function * show(){
        yield 'welcome';
        yield 'to';
        yield 'here';
        yield 'ou';
        return '结束了'
      }
      // 调用
      let newShow = show();
      
      // var done = false;
      // while(!done){
      //   var {value,done} = newShow.next();
      //   console.log(value) // welcome to here ou 结束了
      // };
      console.log("=======================================");
      // for...of没有调用return
      for(let val of newShow){
        console.log(val); // welcome to here ou
      }
      // 配合解构和扩展运算符
      console.log("=======================================");
      let [a,b] = show();
      console.log(a,b); // welcome to
      let [c,...d] = show();
      console.log(c,d); // welcome (3) ["to", "here", "ou"]
      console.log(...show()); // welcome to here ou
      console.log("=======================================");
      //配合Array.from
      console.log(Array.from(show())); // (4) ["welcome", "to", "here", "ou"]

      




    </script>
    <!-- built files will be auto injected -->
  </body>
</html>
Last modification:June 18th, 2019 at 10:21 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment