es6常用语法

// 箭头函数 
let f = a => a + 1;
// 等价于
var f = function(a) {
    return a + 1;
}

// 模版字符串
let a = 'react'
let s = `hello ${a}`; // hello react


// 解构赋值
let [lang, ...other] = ['js', 'java', 'react']; // js , ['java', 'react']

// 扩展运算符 合并数组
let arr1 = ['a', 'a1'];
let arr2 = ['b'];
let newArr = [...arr1, ...arr2] // ['a', 'a1', 'b']

// 对象解构
let {
    person: {
        name,
        age
    },
    foo
} = {
    person: {
        name: 'tom',
        age: 4
    },
    foo: 'foo'
}
console.log(name, foo, age) // tom , foo , 4

// 默认值
let t = (a = 1) => a + 2
t(2) // 4
t() // 3

let o = '';
let temp = o || 1 // 1

// map用法
arr1.map(item => (
    item
))

// filter
arr1.filter(item => (item != 'a')) // a1

// swap 变量值交换
let foo = 1
let bar = 2

;
[foo, bar] = [bar, foo] // foo = 2 , bar = 1

 

你可能感兴趣的:(Javascript)