ES6 introduced the destructuring assignment syntax that supports unpacking values from objects:
let { r = 0, g = 0, b = 0 } = { r: 255, g: 55 };
let color = { r, g, b };
console.log(color, r, g, b); // prints "{r: 255, g: 55, b: 0} 255 55 0"
However, the following snippet would not have the same effect:
let color = { r = 0, g = 0, b = 0 } = { r: 255, g: 55 }
console.log(color); // prints "{r: 255, g: 55}"
Same if you reverse the order:
let { r = 0, g = 0, b = 0 } = color = { r: 255, g: 55 }
console.log(color); // prints "{r: 255, g: 55}"
Is there a one-line solution to assign r
, g
, b
, along with color
?