🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` const person = { firstName, lastName, } ``` ### 等价于 ``` const person = { firstName:firstName, lastName:lastName, } ``` ### **使用解构赋值给变量赋值** ``` const person = { firstName: 'Stephen', lastName: 'Curry', }; const {firstName, lastName} = person; //本质{firstName: firstName, lastName: lastName} = { firstName: 'Stephen', lastName: 'Curry',} console.log(firstName);//Stephen console.log(lastName);//Curry ``` ### **参数解构** ``` function sayName({ firstName, lastName }) { //本质 { firstName: firstName, lastName: lastName } = { firstName: 'Stephen', lastName: 'Curry'} //在参数传递进来的时候进行了解构赋值,所以可以直接访问firstName, lastName 这两个变量 console.log( firstName + ' ' + lastName );//Stephen Curry } let person = { firstName: 'Stephen', lastName: 'Curry' } sayName(person); // Stephen Curry ``` 实践中,我们会经常用到 ES6 的**参数解构**来简化代码(特别是我们需要调用`commit`很多次的时候): ~~~ actions: { increment ({ commit }) { //本质 { commit:commit } = { commit :function(){},.... } commit('increment') } } ~~~