ES6特性

来源:互联网 发布:windows安装盘 编辑:程序博客网 时间:2024/05/01 08:34

let:块级作用域

例:let name='jack'

while(true){

let name = 'sam'

console.log(name)//sam

break

}

console.log(name)//jack


const:声明常量


class,extends,super:相当于ES5的原型,继承,构造函数

例:class Animal{

constructor(){//构造方法

this.name = 'animal'

}

say(x){

console.log(this.name+' says '+x)

}

}

let animal = new Animal()

animal.say('hi')//animal says hi


class Cat extends Animal{ //继承

constructor(){

super() //指向父类this对象

this.name = 'cat'

}

}

let cat = new Cat()

cat.says('hi')cat says hi

arrow function:箭头函数=>

例:function(x,y){

x++

y--

return x+y

}

等同于:(x,y) => {x++;y--;return x+y}


另:在setTimeout中this指全局变量

① var self = this

② function (){}.bind(this)

③ setTimeout( () => {……},1000)


template String

例:$("#id").append(` //返引号,添加内容直接显示为如下(带空格和换行符)

xxx

xxxx

`)


对象结构

例:let cat = 'tom'

let dog = 'sam'

let zoo={cat,dog}

console.log(zoo)//Object{cat:"tom",dog:"sam"}


反过来:let zoo ={cat:'tom',dog:'sam'}

let{cat,dog} = zoo

console.log(cat,dog)//tom,sam


default函数默认值

例:function animal(x){

x = x || 'cat' 

}

等同于:function animal(x = 'cat'){……}


rest

例:function animals(...x){

console.log(x)

}

animals('cat','dog','fish')//(3)["cat","dog","fish"]

原创粉丝点击