小编Jar*_*ard的帖子

ES6 命名对象参数解构

我目前正在使用对象解构模式和ES6对象解构默认参数的答案中描述的默认参数

(function test({a = "foo", b = "bar"} = {}) {
  console.log(a + " " + b);
})();
Run Code Online (Sandbox Code Playgroud)

我希望能够访问对象参数,而无需将其分配给函数体中的变量并显式列出每个键。

(function test({a = "foo", b = "bar"} = {}) {
  console.log(a + " " + b);
})();
Run Code Online (Sandbox Code Playgroud)

我尝试命名对象参数,但该函数失去了将丢失的键解析为其默认值的能力。

(function test({a = "foo", b = "bar"} = {}) {
  const options = {a, b};
  console.log(options);
})();
Run Code Online (Sandbox Code Playgroud)

在解构为命名参数时,它似乎忽略了默认参数。

这是 ES6 规范的一部分吗?有没有办法在函数体中无需额外代码即可实现所需的行为?


编辑:我删除了一个没有为问题添加上下文的多余示例。

javascript arguments destructuring default-arguments ecmascript-6

6
推荐指数
1
解决办法
1069
查看次数