Es2015 Sybol、generator


<!DOCTYPE html>
<html lang="en">
    <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=""></script>
      <strong>We're sorry but es6test doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    <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 = {
      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} =;
      //   console.log(value) // welcome to here ou 结束了
      // };
      // for...of没有调用return
      for(let val of newShow){
        console.log(val); // welcome to here ou
      // 配合解构和扩展运算符
      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(; // welcome to here ou
      console.log(Array.from(show())); // (4) ["welcome", "to", "here", "ou"]


    <!-- built files will be auto injected -->
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