Monday, May 20, 2024
162
rated 0 times [  164] [ 2]  / answers: 1 / hits: 44876  / 10 Years ago, sun, october 26, 2014, 12:00:00

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?


More From » ecmascript-6

 Answers
8

Yes. 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.


[#69009] Thursday, October 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raymondd

Total Points: 620
Total Questions: 112
Total Answers: 94

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
raymondd questions
Thu, Apr 22, 21, 00:00, 3 Years ago
Thu, Jul 9, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
Thu, Jul 25, 19, 00:00, 5 Years ago
;