es6语法解构赋值以及解构赋值作用

1、对对象的解构

 let person = {
                name: 'xiaoliang',
                age: 18,
                hobby: 'sing'
            }
            // es5
            // let name = person.name;
            // let age = person.age;

es6

  let {
            // name,不完全解构
            age,
            hobby,
        } = person
        console.log(age, hobby);
        let {
            name,
            ...res //剩余参数
        } = person
        console.log(res); // 用对象包裹起来了


 //默认值
        let {
            a,
            b = 20
        } = {
            a: 30
        }
        console.log(a);

2、对数组的解构

 let arr = [1, 2, 3];
        let [m, n] = arr;
        console.log(m, n);
        // 可嵌套
        let [i, [j, l],
            [k]
        ] = [1, [2, 5],
            [3]
        

你可能感兴趣的:(es6,javascript,前端,vue.js)