函数参数对象解构导致未定义的变量

Function parameter object destructuring results in undefined variables

我是运行 Node v6.6.0,支持解构函数参数:

function foo ({ a: { b }}) {
  // stuff
}

假设我想解构 并访问 ab。遗憾的是,以下 似乎有效:

function foo ({ a: { b }}) {
  return [a, b]
}
foo({ a: { b: 123 }})
// ReferenceError: a is not defined!

这是 Node 中的错误还是 ES6 的预期行为? ab 不应该都在函数中定义吗?如果不是,为什么解构具有取消定义基础 属性 名称 (a) 的效果?

有没有一种方法可以使用参数解构 来获得函数中定义的ab?我明确试图避免手动解构它们。

Is this bug in Node or is this the expected behavior for ES6?

这是预期的行为。因为 {a: {b}} 没有将 a 绑定为名称,它只是表示您想要访问解构对象的 a 属性。

您可以使用以下内容。

function foo ({ a, a: {b} }) {
  return [a, b]
}
console.log(foo({ a: { b: 123 }}))