【发布时间】:2017-03-16 21:24:41
【问题描述】:
我有:
const props = {
gallery: [],
select: () => null,
one: 1,
two: 2,
}
我可以使用以下方法对其进行解构:
const {gallery, select, ...other} = props
我现在将有三个变量:
-
图库 =
[] -
选择 =
() => null -
其他 =
{one: 1,two: 2}
是否可以分解为指定的分组?
类似这样的事情(这行不通,但我希望很清楚我正在尝试做什么):
const {{gallery, select}: specific, ...other} = props
所以我将有 2 个变量:
-
具体 =
{gallery: [], select: () => null} -
其他 =
{one: 1,two: 2}
我可以在更高级别解决它并以这种方式构建道具:
const props = {
specific: {
gallery: [],
select: () => null,
},
other: {
one: 1,
two: 2,
}
}
但我只是想知道这是否可以通过解构实现。
【问题讨论】:
-
可能可以使用spread properties 完成,但这在 ES6 中尚不可用
-
@SamiKuhmonen 它永远不会在 ES6 中可用。 ES6 规范已经完成。
-
@Gothdo 措辞含糊,我的意思是它在 ES 版本 6 中尚不可用,但可能会在以后的版本中。
-
“我可以用:” 不使用 ES6。或任何其他指定版本的 ES。
...other(还)不是任何现有 ES 标准中的有效表达式。 -
我更新了问题以提及 ES7 而不是 ES6,因为事实上,对象休息语法在 ES6 中不可用,并且建议用于 ES7。
标签: javascript ecmascript-6 destructuring ecmascript-next