ES6常用功能

·let/const
·多行字符串/模板变量
·解构赋值
·块级作用域
·函数默认参数
·箭头函数
·模块化
·class
·promise

1、let/const
let定义变量,可以被重新赋值。const定义常量,不能被重新赋值。
2、多行字符串/模板变量

//JS
var name =  'zhangsan', age = 20,html = '';
html += '
'; html += '

' + name + '

'; html += '

' + age + '

'; html += '
'; //ES6 const name = 'zhangsan',age = 20; const html = `

${name}

${age}

`; console.log(html);

3、解构赋值

//JS
var obj = {a:100,b:200}
var a = obj.a
var b = obj.b

var arr = ['xxx','yyy','zzz']
var x = arr[0]

//ES6
const obj = {a:10,b:20,c:30}
const {a,c} = obj
console.log(a)
console.log(c)

const arr = ['xxx','yyy','zzz']
const [x,y,z] = arr
console.log(x)
console.log(y)
console.log(z)

4、块级作用域

var obj = {a:100,b:200}
for(var item in obj){
  console.log(item)
}
console.log(item)  //'b'

//ES6
const obj = {a:100,b:200}
for(let item in obj){
  console.log(item)
}
console.log(item)  //undefined

5、函数默认参数

function(a,b=0){
}

6、箭头函数

const arr = [1,2,3]
arr.map(item => item + 1);
arr.map((item,index) => {
console.log(index)
return item + 1
})

你可能感兴趣的:(ES6常用功能)