ES6 学习1

https://www.jianshu.com/p/287e0bb867ae

1,let表示变量、const表示常量。let和const都是块级作用域。一个在函数内部,一个在代码块内部;

 const name = 'lux'

 name = 'joe' //再次赋值此时会报错

 

,2,

    //es5 
    var name = 'lux'
    console.log('hello' + name)
    //es6
    const name = 'lux'
    console.log(`hello ${name}`) //hello lux

3,箭头函数
  • 不需要function关键字来创建函数
  • 省略return关键字
  • 继承当前上下文的 this 关键字
//例如:
    [1,2,3].map( x => x + 1 )
    
//等同于:
    [1,2,3].map((function(x){
        return x + 1
    }).bind(this))

4.拓展的对象功能

ES5我们对于对象都是以键值对的形式书写,是有可能出现键值对重名的。例如:

    function people(name, age) {
        return {
            name: name,
            age: age
        };
    }

键值对重名,ES6可以简写如下:

    function people(name, age) {
        return {
            name,
            age
        };
    }

5.更方便的数据访问--解构

//对象 const people = { name: 'lux', age: 20 }
const { name, age } = people
console.log(`${name} --- ${age}`)
//数组 const color = ['red', 'blue']
const [first, second] = color
console.log(first) //'red'
console.log(second) //'blue'




posted @ 2018-03-08 20:39  ThisCall  阅读(126)  评论(0编辑  收藏  举报