【发布时间】:2017-05-17 14:23:21
【问题描述】:
所以在 MDN 文档中解构函数默认值它给出了下面的例子。
function drawES2015Chart({size = 'big', cords = {x: 0, y: 0}, radius =
25} = {}) {
console.log(size, cords, radius);
// do some chart drawing
}
drawES2015Chart({
cords: {x: 18, y: 30},
radius: 30
});
但是我可以在第一行作为function drawES2015Chart({size = 'big', cords = {x: 0, y: 0}, radius =
25}) 运行这个示例
所以省略了={} 部分。我不确定这为什么有效,如果较短的形式实际上同样正确,那么使用较长的形式会有什么好处。
【问题讨论】:
-
当你在你的版本中传入
nil/undefined/nothing 会发生什么? (例如,没有= {}默认值。) -
@DaveNewton 在传递
null时仍然会出现异常,因为它不会触发默认值替换
标签: javascript ecmascript-6 destructuring