ES6新特性

来源:互联网 发布:图书编辑有前途么 知乎 编辑:程序博客网 时间:2024/06/05 21:18
1、在es6(es2015)中使用let来定义变量:let a=1
2、const来定义常量:const a=1
3、es6引入了类的新概念,与java的面向对象编程类似:
class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        console.log(this.type + ' says ' + say)
    }
}


let animal = new Animal()
animal.says('hello') //animal says hello


class Cat extends Animal {
    constructor(){
        super()
        this.type = 'cat'
    }
}


let cat = new Cat()
cat.says('hello') 
4、cs6最常用的新特性,简化方法定义方式
function(i){ return i + 1; } //传统
(i) => i + 1 //ES6
5、template string也是es6的新特性
$("#result").append(
  "There are <b>" + basket.count + "</b> " +
  "items in your basket, " +
  "<em>" + basket.onSale +
  "</em> are on sale!"
);//传统
$("#result").append(`
  There are <b>${basket.count}</b> items
   in your basket, <em>${basket.onSale}</em>
  are on sale!
`);//最新
用反引号(\)来标识起始,用${}来引用变量
原创粉丝点击