ES6中新增了什么?

1、定义变量加入了 let const

        let 会将变量提升到块顶部(而非像ES5是函数顶部)。

        但是在变量声明前引用变量会造成错误。

        let 是块作用域的,不可以在声明前使用。

        const  如果不希望变量的值在发生改变,使用const声明变量。

2、封闭空间

        为了避免污染全局环境污染不必再使用封闭空间,在局部环境中使用let来定义变量就解决了所有问题

    {        let  a=5;    }

    alert(a);  //undefined

3、字符串和变量的拼接

        1》单行字符串和变量的拼接

        字符串和变量的拼接,在拼接的整个作用域内加``(就是键盘1前面那个键的反引号),变量和字符串都不再加'',变量使用${变量名};

        constname=jason;

        constage=18;

        alert(`His name is ${name},He is ${age}`);

2》多行字符串

    constoLi=`<li>

          <div>我是ES6</div>

          <p>这是我的拼接</p>

     </li>`

4、解构赋值

        1》数组的操作

constarr=[1,2,3,4,5];

 const[s,,,,n]=arr;

  alert(s,n);

        2》值的对调:例如冒泡排序中的数组的值的对调

        function BubbleSort(arr){

                for(var i=0;i<arr.length;i++){

                    for(var j=0;j<arr.length-1;j++){

                        if(arr[j]>arr[j+1]){

                            [arr[j],arr[j+1]]=[arr[j+1],arr[j]];

                        }

                    }

                }

                return arr;

            }

        3》查找返回值(包含多个值的时候)

        function obj(){

                constleft=1, right=2, top=3, bottom=4;

                return    {left,right,top,bottom};

            }

            const{left,bottom}=obj();

            console.log(left, bottom); // 1 4

5、类和面向对象

ES6中引入了类(class)来代替构造函数(constructor)

    1》面向对象:ES6中引入了类(class)

        class Person {

              constructor(name) {

                this.name = name;

              }

              showName() {

                return this.name;

              }

            }

            const P1 = new Person('jason');

            alert(P1.showName()); //jason

        2》继承:提供了新的关键字 extends 和 super

        class Person {

                constructor(name,age) {

                  this.name = name;

                  this.age=age;

                }

                showName() {

                  return this.name;

                }

                showAge(){

                  return this.age;

                }

            }

            class Worker extends Person {

                  constructor(name,age,job){

                      super(name,age);

                      this.job=job;

                  }

                  showJob(){

                      return this.job;

                  }

            }

            const W1 = new Worker('jason',18,'要饭的');

            alert(W1.showJob()); // 要饭的

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容