admin管理员组文章数量:1136175
I'm trying to figure out if there's a way to use object destructuring of default parameters without worrying about the object being partially defined. Consider the following:
(function test({a, b} = {a: "foo", b: "bar"}) {
console.log(a + " " + b);
})();
I'm trying to figure out if there's a way to use object destructuring of default parameters without worrying about the object being partially defined. Consider the following:
(function test({a, b} = {a: "foo", b: "bar"}) {
console.log(a + " " + b);
})();
When I call this with {a: "qux"}
, for instance, I see qux undefined
in the console when what I really want is qux bar
. Is there a way to achieve this without manually checking all the object's properties?
1 Answer
Reset to default 263Yes. You can use "defaults" in destructuring as well:
(function test({a = "foo", b = "bar"} = {}) {
console.log(a + " " + b);
})();
This is not restricted to function parameters, but works in every destructuring expression.
本文标签: javascriptES6 Object Destructuring Default ParametersStack Overflow
版权声明:本文标题:javascript - ES6 Object Destructuring Default Parameters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736923503a1956542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论